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

[MISC] Defer everything to parse() #238

Merged
merged 3 commits into from
Feb 27, 2024
Merged

Conversation

eseiler
Copy link
Member

@eseiler eseiler commented Feb 12, 2024

  • Defers everything to parse()
  • Refactors parse()
  • Refactors init()
  • No options/positional options allowed for subcommands (Follow-up)
  • Fixes --export-helpXhtml being accepted for any X

Follow-up:

  • Question: YES: Are options and flags allowed for the top-level-parser when there are subcommands?
  • Question: NO: Must there always be a subcommand?
      sharg::parser parser{"./test_parser", {"-o", "true"}, /* update off */, {"foo"}};
      parser.add_option(flag_value, sharg::config{.short_id = 'o'};
      parser.parse(); // Throws because no special format + there are subcommands -> subcommand is required.
    It's currently a sharg::user_input_error (verify_subcommand()).
    I don't think it's necessary. It might be hard to differentiate between options/flags for the top-level-parser and a potentially misspelled subcommand.
  • Add misspelled subcommand check back. Potentially via catching too_many_arguments from format_parse.
  • subcommand is option value, e.g., ./app -o foo, where foo is a subcommand and -o is either a flag or an option
  • Question: Commit 3Deferred: Do we want something like a std::monostate for the format variant?¹ To get rid of the special_format_was_set, I could also just use any non-special-format as default for the variant. But format_empty might be more expressive and help to detect errors.

¹ Can't use std::monostate directly because the visit call needs to be valid for all possible variants.

@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 12, 2024
Copy link

vercel bot commented Feb 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
sharg-parser ✅ Ready (Inspect) Visit Preview Feb 27, 2024 2:08pm

@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 12, 2024
@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 12, 2024
Copy link

codecov bot commented Feb 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.13%. Comparing base (8366490) to head (d44ad98).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #238      +/-   ##
==========================================
- Coverage   95.23%   95.13%   -0.10%     
==========================================
  Files          18       18              
  Lines        1700     1728      +28     
==========================================
+ Hits         1619     1644      +25     
- Misses         81       84       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@h-2
Copy link
Member

h-2 commented Feb 15, 2024

I like it :)

@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 15, 2024
@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 15, 2024
@eseiler eseiler marked this pull request as ready for review February 15, 2024 14:19
@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 15, 2024
@h-2
Copy link
Member

h-2 commented Feb 15, 2024

Commit 2: Are options and flags allowed for the top-level-parser when there are subcommands?

Why not? E.g. git

@eseiler
Copy link
Member Author

eseiler commented Feb 15, 2024

Commit 2: Are options and flags allowed for the top-level-parser when there are subcommands?

Why not? E.g. git

I do think it should be allowed; why shouldn't it be :)

But the code and documentation don't agree on what is allowed.

If I recall correctly from the code, there's also some very weird behavior:

Let's say I have a parser with build as subcommand, and the top-level parser has an option --name <arg>:

./app --name foo // OK
./app build // OK
./app --name foo build // throws

@h-2
Copy link
Member

h-2 commented Feb 16, 2024

./app --name foo // OK
./app build // OK
./app --name foo build // throws

All of these should work in my opinion.


auto check_error = [&parser, &expected_message](char const * file, int line)
{
::testing::ScopedTrace trace(file, line, "check_error");
Copy link
Member Author

Choose a reason for hiding this comment

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

The output looks like this:

../../../../test/unit/parser/format_parse_test.cpp:701: Failure
Expected equality of these values:
  expected_message
    Which is: "[...]"
  actual_message
    Which is: "[...]"
With diff:
@@ -1,3 +1,2 @@
-[...]
-[...]
+[...]

Google Test trace:
../../../../test/unit/parser/format_parse_test.cpp:719: check_error

Without the trace, it would report line 701, i.e., the EXPECT_EQ in the lambda.
Not very helpful.

The trace adds the last part, which then correctly points to line 719.

parser = /* ... */;
{
    SCOPED_TRACE("test1");
    check_error();
}

parser = /* ... */;
{
    SCOPED_TRACE("test2");
    check_error();
}

Would also work instead, but too many scopes for my test.

We might unify the check_error thing at some point since we use it in multiple locations.

@seqan-actions seqan-actions added the lint [INTERNAL] signals that clang-format ran label Feb 22, 2024
@seqan-actions seqan-actions removed the lint [INTERNAL] signals that clang-format ran label Feb 22, 2024
@eseiler eseiler requested a review from smehringer February 22, 2024 14:41
@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 22, 2024
Comment on lines 544 to 546
auto parser = get_subcommand_parser({"-h"}, {"sub1", "sub2"});
parser.info.description.push_back("description");
parser.add_option(option_value, sharg::config{.short_id = 'f', .long_id = "foo", .description = "foo bar."});
Copy link
Member

Choose a reason for hiding this comment

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

sorry, whats wrong here now? I remember you said it must be deleted but I forget and can't see why

Copy link
Member Author

Choose a reason for hiding this comment

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

Originally because options are no longer allowed.
However, I now changed it to just use a flag; and also changed The following options below to The following options

@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 27, 2024
@seqan-actions seqan-actions added lint [INTERNAL] signals that clang-format ran and removed lint [INTERNAL] signals that clang-format ran labels Feb 27, 2024
@eseiler eseiler merged commit 64399ce into seqan:main Feb 27, 2024
33 checks passed
@eseiler eseiler deleted the misc/defer_init branch February 27, 2024 14: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.

4 participants