larryhastings / appeal

Command-line parsing library for Python 3.
Other
128 stars 7 forks source link

"--" can not be passed as argument #17

Closed rschmidtner closed 2 months ago

rschmidtner commented 11 months ago

Why does python script.py f "--" not work?

# script.py
import appeal

app = appeal.Appeal()

@app.command()
def f(a):
    print(f"Received the following parameters:")
    for key, value in locals().items():
        print(f"  name: {key}")
        print(f"    value: {value}")
        print(f"    type: {type(value)}")

if __name__ == "__main__":
    app.main()
larryhastings commented 2 months ago

"--" is a standard convention for command-line processing. It means "stop handling options for all subsequent command-line arguments". Consider: what if you wanted to use grep to find the string -i in a file? You'd have to somehow convince grep to not read that as "perform a case-insensitive search". You'd do that with "--".

In the case of Appeal, Appeal itself consumes the "--"; it doesn't pass it on to a converter. But all subsequent command-line arguments are passed on as positional arguments, even if they start with dashes.

With your sample program, if you wanted a to get the value -- from the command-line, just specify it twice:

% python script.py f -- --

(It's not necessary to put quote marks around the two dashes.)

Or to pass in -a or --longfellow to a:

% python script.py f -- -a
% python script.py f -- --longfellow