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

Double-dash -- parsing scenario #343

Closed
brunk opened this issue Jul 29, 2024 · 1 comment
Closed

Double-dash -- parsing scenario #343

brunk opened this issue Jul 29, 2024 · 1 comment

Comments

@brunk
Copy link

brunk commented Jul 29, 2024

Thank you very much for argc. I have adopted it as my "go-to" command parser.

Summary

I have encountered a use case related to issue #251, in which parsing the command line like GNU getopt would be helpful. I, too, want to use argc to write a wrapper around another CLI tool to which I may wish to pass additional arguments and flags.

The issue arises when there are other arguments defined in addition to # @arg remaining_args~

Environment

MacOS Darwin Kernel Version 23.5.0: arm64
argc 1.19.0

Description

My sample script double-dash-tester.sh appears as follows:

#!/usr/bin/env bash

# @describe Testing out the use of double-dash to stop parsing.
# @flag -t --test Test flag
# @arg first! Search query
# @arg second=/tmp
# @arg remaining_args~

main () {
    echo -n "    "; declare -p argc_test
    echo -n "    "; declare -p argc_first
    echo -n "    "; declare -p argc_second
    echo -n "    "; declare -p argc_remaining_args
}

argc_test=""
argc_remaining_args=()
eval "$(argc --argc-eval "$0" "$@")"

With argc 1.19.0, I get the following output using a variety of flags:

$ ./double-dash-tester.sh -- 
error: the following required arguments were not provided:
  <FIRST>

$ ./double-dash-tester.sh -- first second
    declare -- argc_test=""
    declare -- argc_first="first"
    declare -- argc_second="second"
    declare -a argc_remaining_args='()'

$ ./double-dash-tester.sh first -- second
    declare -- argc_test=""
    declare -- argc_first="first"
    declare -- argc_second="second"
    declare -a argc_remaining_args='()'

$ ./double-dash-tester.sh first second -- -t
    declare -- argc_test=""
    declare -- argc_first="first"
    declare -- argc_second="second"
    declare -a argc_remaining_args='([0]="--" [1]="-t")'

My reading of GNU Standards for Command Line Interfaces and the getopt sample code is that argument parsing should terminate upon finding a --.

I would anticipate the following results if parsing terminated on --:

$ ./double-dash-tester.sh -- first second
error: the following required arguments were not provided:
  <FIRST>

$ ./double-dash-tester.sh first -- second
    declare -- argc_test=""
    declare -- argc_first="first"
    declare -- argc_second="/tmp"
    declare -a argc_remaining_args='([0]="--" [1]="second")'

$ ./double-dash-tester.sh first second -- -t
    declare -- argc_test=""
    declare -- argc_first="first"
    declare -- argc_second="second"
    declare -a argc_remaining_args='([0]="--" [1]="-t")'

I don't assume that the GNU getopt behavior is the goal, but it would be helpful in my use case since I want to pass these arguments after the double-dash on to another command.

@sigoden
Copy link
Owner

sigoden commented Jul 29, 2024

This behavior is expected.

The -- in argc means that all remaining arguments are positional and should not be parsed as options. There is no other meaning besides this.

If you prefer using --, you don't even have to use ~.

- # @arg remaining_args~
+ # @arg remaining_args*
$ ./double-dash-tester.sh first second -- -t
    declare -- argc_test=""
    declare -- argc_first="first"
    declare -- argc_second="second"
    declare -a argc_remaining_args=([0]="-t")

If you have used ~, you don't need to pass --.

$ ./double-dash-tester.sh first second -t
    declare -- argc_test=""
    declare -- argc_first="first"
    declare -- argc_second="second"
    declare -a argc_remaining_args=([0]="-t")

@sigoden sigoden closed this as completed Jul 29, 2024
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

2 participants