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

Support case-insensitive usage #426

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions fire/core.py
Expand Up @@ -515,12 +515,27 @@ def _Fire(component, args, parsed_flag_args, context, name=None):
else:
component_dict = component

lowered_component_dict = dict((str(k).lower(), str(v).lower())
for k,v in component_dict.items())

if target in component_dict:
component = component_dict[target]
handled = True
elif target.replace('-', '_') in component_dict:
component = component_dict[target.replace('-', '_')]
handled = True
elif target in lowered_component_dict:
for key, value in component_dict.items():
if target == str(key).lower():
component = component_dict[key]
handled = True
break
elif (target.lower()
in lowered_component_dict) and target != target.lower():
print('NOTE: Consider using the correct capitalization for {}'.format(
target))
error = FireError('Ambiguous component access:', target)
candidate_errors.append((error, initial_args))
else:
# The target isn't present in the dict as a string key, but maybe it is
# a key as another type.
Expand Down Expand Up @@ -652,6 +667,15 @@ def _GetMember(component, args):
for arg_name in arg_names:
if arg_name in members:
return getattr(component, arg_name), [arg], args[1:]
else:
for member in members:
if arg_name == member.lower():
return getattr(component, member), [arg], args[1:]
# The member exists, but the capitalization is incorrect.
elif arg_name.lower() == member.lower():
print('NOTE: Consider using the correct capitalization for {}'.format(
arg_name))
raise FireError('Ambiguous member access:', arg_name)

raise FireError('Could not consume arg:', arg)

Expand Down
12 changes: 12 additions & 0 deletions fire/fire_test.py
Expand Up @@ -503,6 +503,18 @@ def testSingleCharFlagParsingCapitalLetter(self):
fire.Fire(tc.CapitalizedArgNames,
command=['sum', '-D', '5', '-G', '10']), 15)

def testCaseInsensitiveUsage(self):
self.assertEqual(
fire.Fire(tc.CapitalizedFunctionNames, command=['alpha']), 'alpha')
self.assertEqual(
fire.Fire(tc.CapitalizedFunctionNames, command=['Alpha']), 'Alpha')
self.assertEqual(
fire.Fire(tc.CapitalizedFunctionNames,
command=['beta']), 'Beta')
with self.assertRaisesFireExit(2):
# Ambiguous member access
fire.Fire(tc.CapitalizedFunctionNames, command=['ALPHA'])

def testBoolParsingWithNo(self):
# In these examples --nothing always refers to the nothing argument:
def fn1(thing, nothing):
Expand Down
7 changes: 7 additions & 0 deletions fire/test_components.py
Expand Up @@ -148,6 +148,13 @@ class CapitalizedArgNames(object):
def sum(self, Delta=1.0, Gamma=2.0): # pylint: disable=invalid-name
return Delta + Gamma

class CapitalizedFunctionNames(object):
def alpha(self):
return 'alpha'
def Alpha(self):
return 'Alpha'
def Beta(self):
return 'Beta'

class Annotations(object):

Expand Down