From 6d7b2e12e1c472642d55fb08d296c14bb7a2067f Mon Sep 17 00:00:00 2001 From: Justin Mayfield Date: Tue, 18 Apr 2017 16:46:15 -0600 Subject: [PATCH] Support positional args with dashes --- CHANGELOG.md | 3 +++ shellish/command/supplement.py | 4 ++++ test/command.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c45db23..f1c5815 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] - unreleased +### Fixed +- Fix for support of positionals with dashes in them. They are converted + to underscores just like they are for optionals. ## [5] - 2017-04-10 diff --git a/shellish/command/supplement.py b/shellish/command/supplement.py index 13b688b..c9bae58 100644 --- a/shellish/command/supplement.py +++ b/shellish/command/supplement.py @@ -362,6 +362,10 @@ def add_subparsers(self, prog=None, **kwargs): prog = f.format_help().strip() return super().add_subparsers(prog=prog, **kwargs) + def _get_positional_kwargs(self, dest, **kwargs): + dest = dest.replace('-', '_') + return super()._get_positional_kwargs(dest, **kwargs) + class SafeFileContext(object): """ Used by SafeFileType to provide a file-like context manager. """ diff --git a/test/command.py b/test/command.py index 007e2d7..3c04363 100644 --- a/test/command.py +++ b/test/command.py @@ -153,3 +153,20 @@ def run(args): cmd = shellish.Command(name='test', run=run) cmd.add_file_argument('--foo', mode='w') cmd(argv='--foo makethis') + + +class ArgumentDestSafety(unittest.TestCase): + + def test_dash_positional(self): + c = shellish.Command(name='test_dash') + c.add_argument('foo-bar') + args = c.argparser.parse_known_args(['value'])[0] + self.assertIn('foo_bar', args) + self.assertEqual(args.foo_bar, 'value') + + def test_dash_opt(self): + c = shellish.Command(name='test_dash') + c.add_argument('--foo-bar') + args = c.argparser.parse_known_args(['--foo-bar', 'value'])[0] + self.assertIn('foo_bar', args) + self.assertEqual(args.foo_bar, 'value')