-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Update template code, documentation page for the DNF5 Command Template #1994
Changes from all commits
7bc081a
4a5e437
d58e7e2
17086a8
1895ce1
da5f314
8a2e0b8
8ee2c3f
d8248e7
7fe21f1
9c5112c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Adjust :lines: in template-command.rst if removing/modifying these comment lines! | ||
// clang-format off | ||
#include "template.hpp" | ||
|
||
#include <iostream> | ||
|
@@ -6,6 +8,15 @@ namespace dnf5 { | |
|
||
using namespace libdnf5::cli; | ||
|
||
void TemplateCommand::set_parent_command() { | ||
auto * parent_cmd = get_session().get_argument_parser().get_root_command(); | ||
auto * this_cmd = get_argument_parser_command(); | ||
parent_cmd->register_command(this_cmd); | ||
|
||
auto & group = parent_cmd->get_group("software_management_commands"); | ||
group.register_argument(this_cmd); | ||
} | ||
|
||
void TemplateCommand::set_argument_parser() { | ||
// Context is the main object in dnf5. | ||
// It contains useful functions and pieces of information necessary to run | ||
|
@@ -16,37 +27,42 @@ void TemplateCommand::set_argument_parser() { | |
auto & ctx = get_context(); | ||
auto & parser = ctx.get_argument_parser(); | ||
|
||
auto & cmd = *get_argument_parser_command(); | ||
auto & this_cmd = *get_argument_parser_command(); | ||
|
||
// Add template command to the Parser | ||
// Configure 'template' command in the Parser | ||
// | ||
// Set a description that would be displayed in the second column of the | ||
// help message would also add the command to the parser | ||
cmd.set_short_description("A command that prints its name and arguments' name"); | ||
// Set a description that would be displayed in the second column of | ||
// the main help message. | ||
this_cmd.set_description("A command that prints its name and arguments' name"); | ||
|
||
// Add foo and bar options | ||
// Add '--foo' and '--bar' options | ||
// | ||
// Option 1: as said in the header file here you should handle the pointer | ||
// by giving up the ownership to dnf5's parser. | ||
// Set the default value here. | ||
foo_option = dynamic_cast<libdnf5::OptionBool *>( | ||
parser.add_init_value(std::unique_ptr<libdnf5::OptionBool>(new libdnf5::OptionBool(false)))); | ||
parser.add_init_value( | ||
std::unique_ptr<libdnf5::OptionBool>( | ||
new libdnf5::OptionBool(false)))); | ||
Comment on lines
43
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kontura My only problem with this (which the pre-commit formatter wants to change back to the un-wrapped format:) foo_option = dynamic_cast<libdnf5::OptionBool *>(
 parser.add_init_value(std::unique_ptr<libdnf5::OptionBool>(new libdnf5::OptionBool(false)))); ...is that it won't fit when it's embedded on the documentation page, without forcing the reader to scroll horizontally. Is there an override comment that can stop the formatter from re-wrapping it? I really think it reads better, for the documentation, if the line length is kept down to fit the page format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do this:
If the disable comment was at the very top of the page we could hide it by changing the include lines again so it doesn't complicate the template. (I have tested it only briefly but I think if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kontura Done, thanks! I think that looks much better. I also made some other changes, I'll add some review comments about those. |
||
|
||
// Create an option by giving it a name. It will be shown in the help message. | ||
// Set long name, description and constant value. | ||
// Link the option to the TemplateCommand's class member. | ||
auto foo = parser.add_new_named_arg("foo"); | ||
// Set the long name for the option. | ||
foo->set_long_name("foo"); | ||
foo->set_short_description("print foo"); | ||
// Set the option's description (for the help message). | ||
foo->set_description("print foo"); | ||
// Set the constant value. This is the value assigned when an option | ||
// which takes no value arguments appears on the command line. | ||
foo->set_const_value("true"); | ||
// Link the option to the TemplateCommand's class member. | ||
foo->link_value(foo_option); | ||
Comment on lines
48
to
58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And, I fleshed out these comments after realizing, as I was reading the code, that I actually had no idea what the "const_value" was — and had to go digging through |
||
|
||
// Register the argument to the command template. | ||
cmd.register_named_arg(foo); | ||
// Register the '--foo' argument to the 'template' command. | ||
this_cmd.register_named_arg(foo); | ||
|
||
// Option 2: create a unique_ptr of type BarOption. | ||
// The long name, description, and other values were set in | ||
// dnf5/command/arguments.hpp | ||
// template.hpp when we derived the BarOption class. | ||
bar_option = std::make_unique<BarOption>(*this); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _dnf5 plugin template: | ||
|
||
DNF5 Plugin Template | ||
==================== | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to rename this to match the variable name in the
set_parent_command()
method, to more clearly show that they represent the same object.