Skip to content

Commit

Permalink
Support positional args with dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mayfield committed Apr 18, 2017
1 parent 484b618 commit 6d7b2e1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions shellish/command/supplement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand Down
17 changes: 17 additions & 0 deletions test/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 6d7b2e1

Please sign in to comment.