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

Custom spellings for CLI options #718

Open
akaihola opened this issue Jan 8, 2019 · 0 comments
Open

Custom spellings for CLI options #718

akaihola opened this issue Jan 8, 2019 · 0 comments

Comments

@akaihola
Copy link
Contributor

akaihola commented Jan 8, 2019

If I define this function:

@hug.cli()
def foo(data: hug.types.text = 'default data', dry_run: hug.types.boolean = False):
    ...

I get these CLI options:

usage: dryrun.py [-h] [-d DATA] [-dr]

optional arguments:
  -h, --help            show this help message and exit
  -d DATA, --data DATA  Basic text / string value
  -dr, --dry_run        Providing any value will set this to true

I would much prefer dry_run to not have a short option, and to have a dash in --dry-run instead of an underscore.

In general, I would like the spelling of CLI options to be customizable.

I found this solution to this particular case, but unfortunately it fiddles with a private ArgumentParser attribute:

def cli(*args, **kwargs):
    option_strings = kwargs.pop('option_strings', {})

    def wrapper(func):
        api_function = hug.cli(*args, **kwargs)(func)
        for action in api_function.interface.cli.parser._actions:
            if action.dest in option_strings:
                action.option_strings = option_strings[action.dest]
        return api_function
    return wrapper

@cli(option_strings={'dry_run': ['--dry-run']})
def foo(data: hug.types.text = 'default data', dry_run: hug.types.boolean = False):
    ...

Can anyone think of a good way to add support for this in Hug?

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