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

Default subcommand inconsistencies #742

Open
2 tasks done
rgoldberg opened this issue Feb 17, 2025 · 0 comments
Open
2 tasks done

Default subcommand inconsistencies #742

rgoldberg opened this issue Feb 17, 2025 · 0 comments

Comments

@rgoldberg
Copy link
Contributor

rgoldberg commented Feb 17, 2025

ArgumentParser version:

main

Swift version:

swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: x86_64-apple-macosx12.0

There seems to be some inconsistencies for how default subcommands behave.

I think they probably stem from applying arguments that could work on the command to the default subcommand instead of to the command.

I would think that all arguments that can apply to the command should be applied to the command until the first argument that cannot be applied to the command. If that argument matches a subcommand name, obviously use the matching subcommand. If it doesn't match a subcommand name, then try to apply that & all subsequent arguments to the default subcommand.

Whatever behavior is chosen will affect how generated shell completion scripts should behave.

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

Using the math testing command:

$ ./.build/debug/math --version    
1.0.0

## the following seems to be trying to use `add` as an argument to the default subcommand `add`,
## but it only accepts integers positionals

$ ./.build/debug/math --version add
Error: The value 'add' is invalid for '<values>'
Help:  <values>  A group of integers to operate on.
Usage: math add [--hex-output] [<values> ...]
  See 'math add --help' for more information.

## the following seems to be trying to use `add` as an argument to the default subcommand `add`,
## but it only accepts integers positionals

$ ./.build/debug/math --version add 1 2
Error: The value 'add' is invalid for '<values>'
Help:  <values>  A group of integers to operate on.
Usage: math add [--hex-output] [<values> ...]
  See 'math add --help' for more information.

$ ./.build/debug/math add --version
1.0.0

$ ./.build/debug/math add
0

$ ./.build/debug/math add 1 2
3

If the default subcommand is removed from the math testing command:

$ ./.build/debug/math --version    
1.0.0

$ ./.build/debug/math --version add
1.0.0

$ ./.build/debug/math --version add 1 2
1.0.0

$ ./.build/debug/math add --version
1.0.0

$ ./.build/debug/math add
0

$ ./.build/debug/math add 1 2
3

Expected behavior

The same output as the examples for math without the default command.

Actual behavior

The output of the examples for math as it is in the codebase, i.e. with add as its default command. Specifically, the 2 errors indicate the issue.

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

No branches or pull requests

1 participant