apple / apple/swift-argument-parser

Default subcommand inconsistencies

Open
#742 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
3.8k
Forks
411
Avg merge
7d 14h
Merged PRs (30d)
15

Description

**ArgumentParser version:**

`main`

**Swift version:**

```shell
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
- [X] If possible, I've reproduced the issue using the `main` branch of this package
- [X] I've searched for [existing GitHub issues](https://github.com/apple/swift-argument-parser/issues)

### Steps to Reproduce

Using the `math` testing command:

```console
$ ./.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 ''
Help: A group of integers to operate on.
Usage: math add [--hex-output] [ ...]
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 ''
Help: A group of integers to operate on.
Usage: math add [--hex-output] [ ...]
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:

```console
$ ./.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.

Contributor guide

Open the contributing guide

Research direction

Start with the math testing command and reproduce the --version cases with and without the default add subcommand. Trace how command-level arguments, subcommand names, and arguments for the default subcommand are assigned, including the impact on generated shell completion scripts. Done means the default-command behavior matches the no-default-command examples without rejecting add as an integer.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.