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

Cli option mutual exclusion not working in all cases #289

Open
drennalls opened this issue Jul 29, 2016 · 0 comments
Open

Cli option mutual exclusion not working in all cases #289

drennalls opened this issue Jul 29, 2016 · 0 comments

Comments

@drennalls
Copy link
Contributor

I have an app with a simple requirement. Allow the user to save some data to a file, with an autogenerated name or a user-specified name. If --out is used then a generated filename (based on some data properties) is used. For a custom filename the user can use --out-file instead. I would have liked to just have one --out option that can either take an argument or be used on it's own, but there doesn't seem to be a way to do that. So sticking to mutually-exclusive --out and --out-file options. However, the mutual-exclusion check doesn't work depending on the order.

..Here's a dummy app to demonstrate the problem.

#!/usr/bin/env python
from plumbum import cli
from pprint import pprint

class MyApp(cli.Application):
    _out_format = cli.SwitchAttr("out-format", cli.Set('json', 'yaml'), default='json',
                                 help="Data format")
    _out_flag = cli.Flag("out", help="Save data using an auto-generated filename",
                         excludes=["out-file"])
    _out_file = cli.SwitchAttr("out-file", str, help="Save data using a specific filename", default=None, excludes=["out"])

    def main(self):
        print("out={}\n,out-file={}\n,out-format={}\n".format(self._out_flag, self._out_file,
                                                             self._out_format))

if __name__ == "__main__":
    MyApp.run()

Usage..

Usage:
    test.py [SWITCHES]

Meta-switches
    -h, --help                               Prints this help message and quits
    --help-all                               Print help messages of all subcommands and quit
    -v, --version                            Prints the program's version and quits

Switches
    --out                                    Save data using an auto-generated filename; excludes --out-file
    --out-file VALUE:str                     Save data using a specific filename; excludes --out
    --out-format VALUE:{'yaml', 'json'}      Data format; the default is 'json'

..Mutual exclusive works in this case..

~/misc/test.py --out --out-file --out-format json
Error: Given --out, the following are invalid ['--out-file']
------
Usage:
    test.py [SWITCHES]

Meta-switches
    -h, --help                               Prints this help message and quits
    --help-all                               Print help messages of all subcommands and quit
    -v, --version                            Prints the program's version and quits

Switches
    --out                                    Save data using an auto-generated filename; excludes --out-file
    --out-file VALUE:str                     Save data using a specific filename; excludes --out
    --out-format VALUE:{'yaml', 'json'}      Data format; the default is 'json'

But not in this case

~/misc/test.py --out-file --out --out-format json
out=False
out-file=--out
out-format=json

The "--out" is eaten up as the argument for --out-file instead of reporting an error.

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