Skip to content

Commit

Permalink
Support single-hyphen flags.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 215983601
Change-Id: I7d7d1323b786bc93f3f311a609e7b8f836011ac4
Reviewed-on: https://team-review.git.corp.google.com/c/330493
Reviewed-by: David Bieber <[email protected]>
  • Loading branch information
dbieber committed Oct 5, 2018
1 parent 5347858 commit 53a3498
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def _ParseKeywordArgs(args, fn_spec):

keyword = ''
if _IsSingleCharFlag(argument):
keychar = argument[1]
keychar = argument.lstrip('-')
potential_args = [arg for arg in fn_args if arg[0] == keychar]
if len(potential_args) == 1:
keyword = potential_args[0]
Expand All @@ -807,7 +807,7 @@ def _ParseKeywordArgs(args, fn_spec):
"refer to any of the following arguments: {}".format(
argument, potential_args))
else:
keyword = argument[2:]
keyword = argument.lstrip('-')

contains_equals = '=' in keyword
is_bool_syntax = (not contains_equals and
Expand Down Expand Up @@ -865,7 +865,7 @@ def _IsSingleCharFlag(argument):

def _IsMultiCharFlag(argument):
"""Determines if the argument is a multi char flag (e.g. '--alpha')."""
return argument.startswith('--')
return argument.startswith('--') or re.match('^-[a-zA-Z]', argument)


def _ParseValue(value, index, arg, metadata):
Expand Down
40 changes: 40 additions & 0 deletions fire/fire_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,30 @@ def testFireNamedArgs(self):
fire.Fire(tc.OldStyleWithDefaults, command=['triple', '--count', '5']),
15)

def testFireNamedArgsSingleHyphen(self):
self.assertEqual(fire.Fire(tc.WithDefaults,
command=['double', '-count', '5']), 10)
self.assertEqual(fire.Fire(tc.WithDefaults,
command=['triple', '-count', '5']), 15)
self.assertEqual(
fire.Fire(tc.OldStyleWithDefaults, command=['double', '-count', '5']),
10)
self.assertEqual(
fire.Fire(tc.OldStyleWithDefaults, command=['triple', '-count', '5']),
15)

def testFireNamedArgsWithEquals(self):
self.assertEqual(fire.Fire(tc.WithDefaults,
command=['double', '--count=5']), 10)
self.assertEqual(fire.Fire(tc.WithDefaults,
command=['triple', '--count=5']), 15)

def testFireNamedArgsWithEqualsSingleHyphen(self):
self.assertEqual(fire.Fire(tc.WithDefaults,
command=['double', '-count=5']), 10)
self.assertEqual(fire.Fire(tc.WithDefaults,
command=['triple', '-count=5']), 15)

def testFireAllNamedArgs(self):
self.assertEqual(fire.Fire(tc.MixedDefaults, command=['sum', '1', '2']), 5)
self.assertEqual(fire.Fire(tc.MixedDefaults,
Expand Down Expand Up @@ -354,6 +372,23 @@ def testBoolParsingContinued(self):
fire.Fire(tc.MixedDefaults, command=['identity', '10', '--beta']),
(10, True))

def testBoolParsingSingleHyphen(self):
self.assertEqual(
fire.Fire(tc.MixedDefaults,
command=['identity', '-alpha=False', '10']), (False, 10))
self.assertEqual(
fire.Fire(tc.MixedDefaults,
command=['identity', '-alpha', '-beta', '10']), (True, 10))
self.assertEqual(
fire.Fire(tc.MixedDefaults,
command=['identity', '-alpha', '-beta=10']), (True, 10))
self.assertEqual(
fire.Fire(tc.MixedDefaults,
command=['identity', '-noalpha', '-beta']), (False, True))
self.assertEqual(
fire.Fire(tc.MixedDefaults,
command=['identity', '-alpha', '-10', '-beta']), (-10, True))

def testBoolParsingLessExpectedCases(self):
# Note: Does not return (True, 10).
self.assertEqual(
Expand Down Expand Up @@ -560,6 +595,11 @@ def testSeparatorForChaining(self):
'--separator', '$$']),
True)

def testNegativeNumbers(self):
self.assertEqual(
fire.Fire(tc.MixedDefaults,
command=['sum', '--alpha', '-3', '--beta', '-4']), -11)

def testFloatForExpectedInt(self):
self.assertEqual(
fire.Fire(tc.MixedDefaults,
Expand Down

0 comments on commit 53a3498

Please sign in to comment.