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

Usage generated by built-in help flag prepends names of OptionGroups to command stack if all arguments are optional #578

Open
2 tasks done
YourMJK opened this issue Jul 13, 2023 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@YourMJK
Copy link

YourMJK commented Jul 13, 2023

The usage generated by the built-in help flag has the name of the type of all @OptionGroup members prepended to the command stack if all options and arguments are optional (i.e. have a default value) and thus differs from the usage in e.g. ParsableCommand.helpMessage().

ArgumentParser version: 1.0.01.2.2 & main
Swift version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)

Checklist

  • If possible, I've reproduced the issue using the main branch of this package
  • I've searched for existing GitHub issues

Steps to Reproduce

  • Create a ParsableCommand with at least one @OptionGroup member and provide default values to all members such that all arguments are optional.
    Example:
struct Test: ParsableCommand {
    struct Group: ParsableCommand {
        @Option
        var num: Int = 0
    }
    
    @OptionGroup
    var options: Group
    
    @Argument
    var arg: String = ""
}
  • Build an executable which runs the ParsableCommand through @main or ParsableCommand.main().
  • Execute the binary with the "-h" or "--help" flag.

Expected behavior

The usage string that is printed as part of the help message is the same as the usage printed when running it with no arguments and the same as returned by Test.helpMessage().

In this case:
USAGE: test [--num <num>] [<arg>]

Actual behavior

The usage string that is printed as part of the help message contains the name of the type of all @OptionGroup members before the actual command name.

The first line printed when executing the binary with only "--help" as an argument is:
USAGE: group test [--num <num>] [<arg>]

and thus differs from the other methods of getting the usage which are all as expected.

If we change our ParsableCommand struct to have at least one non-optional argument by removing one of the default values, the usage returned by the built-in help flag is fixed.
For example, with:

struct Test: ParsableCommand {
    struct Group: ParsableCommand {
        @Option
        var num: Int = 0
    }
    
    @OptionGroup
    var options: Group
    
    @Argument
    var arg: String// = ""
}

the help flag correctly produces:
USAGE: test [--num <num>] <arg>
again.

@YourMJK
Copy link
Author

YourMJK commented Jul 18, 2023

Seems like this issue doesn't occur when you use ParsableArguments instead of ParsableCommand for the @OptionGroup's type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants