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

Application cannot handle arguments to options that start with a dash and are not numbers #658

Closed
maxnoe opened this issue Apr 14, 2021 · 1 comment

Comments

@maxnoe
Copy link
Contributor

maxnoe commented Apr 14, 2021

Minimal example application:

from traitlets.config import Application
from traitlets import Unicode


class TestDash(Application):
    test = Unicode().tag(config=True)
    aliases = {
        "test": "TestDash.test",
    }

    def initialize(self, argv=None):
        self.parse_command_line(argv)

    def start(self):
        print(self.test)


if __name__ == '__main__':
    app = TestDash()
    app.initialize()
    app.start()

Just a number works:

$ python tool.py --test -1
-1

Something else does not:

$ python tool.py --test -1d
usage: tool.py [-h] [--debug] [--show-config] [--show-config-json]
               [--test TestDash.test]
               [extra_args [extra_args ...]]
tool.py: error: argument --TestDash.test: expected one argument

Quoted with space it works again:

❯ python tool.py --test "-1 d"
-1 d

But not without the space:

❯ python tool.py --test "-1d"
-1 d

With =, it always works:

❯ python tool.py --test=-1d
-1d

I am not sure if this is really a traitlets limitation or if there is just no way around this.

@azjps
Copy link
Collaborator

azjps commented Jan 30, 2023

Unfortunately its a long-running issue with the standard library argparse module: some examples of discussions from a decade ago: https://bugs.python.org/issue9334 & https://stackoverflow.com/questions/16174992/cant-get-argparse-to-read-quoted-string-with-dashes-in-it. As you mentioned, --test=-1d or --test " -1d" are possible ways to work around it.

@azjps azjps closed this as completed Jan 30, 2023
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