Skip to content
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

Merged
merged 11 commits into from
Jan 23, 2025

Conversation

ferdnyc
Copy link
Contributor

@ferdnyc ferdnyc commented Jan 8, 2025

Recently on Fedora Discussions, it was brought up / discovered that the template source code in the DNF5 documentation had fallen a bit out of date, to the point where it wasn't even compilable because the internal APIs have changed so much in two years.

Since it actually says, right at the top of that page:

Note

This code is thought to be self explanatory. You should be able to copy the snippets, following the directory structure and naming shown above each one.

...It seemed clear that it was no longer remotely achieving that goal.

So, I went through and updated all of the included source code based on the current dnf5/commands/ source and dnf5/main.cpp, built and tested the template source integrated into DNF5 (as a new dnf5/commands/template/ directory, just like the note says), and also updated the sample output at the end to reflect reality.

I hope to do the same for the other two template documents, as well, but we'll see what happens. I wanted to at least submit this one for now, and I'll take the rest one step at a time.

The `.. literalinclude::`s for the code in `doc/templates/command/`
were using `:lines:` arguments that excluded the first line, which
is the `#ifndef` for the include guards. That's kind of important.
Bring code in `doc/templates/command/` up to date with latest
internal API changes; code tested and working when compiled in
to current HEAD codebase.
- Add a section heading for each file
- Move filenames to listing captions
- Make integration instructions into a section, and add a CAUTION
  admonishment that plugin authors should ignore it
- Link from caution message to next template (DNF5 Plugin Template)
@kontura kontura self-assigned this Jan 20, 2025
Copy link
Contributor

@kontura kontura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have noticed only one minor detail.

This is really great, thank you!

doc/templates/template-command.rst Outdated Show resolved Hide resolved
@kontura
Copy link
Contributor

kontura commented Jan 21, 2025

Plus it seems there is a couple formatting issues, see the Pre Commit action details.

Comment on lines 39 to +42
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))));
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this:

int formatted_code;
// clang-format off
    void    unformatted_code  ;
// clang-format on
void formatted_code_again;

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 // clang-format on is omitted it disables the formatting for the one file only.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@ferdnyc
Copy link
Contributor Author

ferdnyc commented Jan 22, 2025

I have a feeling the formatter is still going to complain about that one line.

(Edit: I was right, so I just surrendered and did what it wants. But I still think it makes the documentation harder to read.)

The formatter wants to unwrap some lines of code to make them longer
than will fit the width of the documentation when embedded.
@@ -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();
Copy link
Contributor Author

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.

Comment on lines 48 to 58
// 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 include/libdnf5-cli/argument_parser.hpp to find out.

@ferdnyc
Copy link
Contributor Author

ferdnyc commented Jan 22, 2025

The RPM failures are nothing to do with me, they're happening because SWIG is generating Ruby code Ruby wrapper code with warnings, and the build is in -Werror mode:

In file included from /usr/include/ruby/internal/stdbool.h:31,
                 from /usr/include/ruby/backward/2/bool.h:22,
                 from /usr/include/ruby/defines.h:74,
                 from /usr/include/ruby/ruby.h:25,
                 from /usr/include/ruby.h:38,
                 from /builddir/build/BUILD/dnf5-5.2.8.1-build/dnf5-5.2.8.1/redhat-linux-build/bindings/ruby/libdnf5/CMakeFiles/ruby_common.dir/commonRUBY_wrap.cxx:912:
/usr/include/c++/15/cstdbool:48:6: error: #warning "<cstdbool> is deprecated in C++17, remove the #include" [-Werror=cpp]
   48 | #    warning "<cstdbool> is deprecated in C++17, remove the #include"
      |      ^~~~~~~
cc1plus: note: unrecognized command-line option ‘-Wno-sometimes-uninitialized’ may have been intended to silence earlier diagnostics
cc1plus: all warnings being treated as errors

Edit: The weird thing is, the Ruby include file in question contains:

#elif defined(__cplusplus)
# /* bool is a keyword in C++. */
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
#  include <cstdbool>
# endif

So it only even tries to #include <cstdbool> in C++11 or higher.

Edit2: That #include was removed last week in ruby/ruby#12573, but hasn't been part of a release yet.

Copy link
Contributor

@kontura kontura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this, it is much appreciated.

I have created an issue that could help with this in the future: #2016.

@kontura kontura added this pull request to the merge queue Jan 23, 2025
Merged via the queue into rpm-software-management:main with commit b0f1caf Jan 23, 2025
11 of 17 checks passed
@ferdnyc ferdnyc deleted the update-doc-templates branch January 23, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants