traitlets icon indicating copy to clipboard operation
traitlets copied to clipboard

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

Open maxnoe opened this issue 4 years ago • 0 comments

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.

maxnoe avatar Apr 14 '21 15:04 maxnoe