Skip to content

Commit

Permalink
Fixes #10 (#70)
Browse files Browse the repository at this point in the history
Adds support for specifying a different command-line name.
  • Loading branch information
loganasherjones committed May 28, 2018
1 parent d25a857 commit 78340e6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
2 changes: 2 additions & 0 deletions docs/usage.rst
Expand Up @@ -353,6 +353,8 @@ For each item in a specification, you can set any of these keys:
+-------------------+------------------+----------------------------------------------------------------------------------------------------------------+
| cli_short_name | ``None`` | One-character command-line shortcut |
+-------------------+------------------+----------------------------------------------------------------------------------------------------------------+
| cli_name | ``None`` | An alternate name to use on the command-line |
+-------------------+------------------+----------------------------------------------------------------------------------------------------------------+
| cli_choices | ``None`` | List of possible values for the item from the command-line |
+-------------------+------------------+----------------------------------------------------------------------------------------------------------------+
| previous_names | ``None`` | List of previous names an item had |
Expand Down
65 changes: 44 additions & 21 deletions tests/items_test.py
Expand Up @@ -270,41 +270,64 @@ def test_update_default(simple_item, current_default, new_default,
assert simple_item.default == expected


@pytest.mark.parametrize('type,required,default,short,args,expected', [
('str', True, 'default_value', None, ['--foo', 'foo_value'], 'foo_value'),
('str', True, 'default_value', None, [], None),
('str', False, None, None, [], None),
('str', True, None, 'f', ['-f', 'foo_value'], 'foo_value'),
('int', True, None, 'f', ['-f', '1'], 1),
('long', True, None, 'f', ['-f', '1'], long(1)),
('float', True, None, 'f', ['-f', '1.23'], 1.23),
('complex', True, None, 'f', ['-f', '2j'], 2j),
@pytest.mark.parametrize('type,required,default,short,name,args,expected', [
(
'str',
True,
'default_value',
None,
None,
['--foo', 'foo_value'],
'foo_value'
),
('str', True, 'default_value', None, None, [], None),
('str', False, None, None, None, [], None),
('str', True, None, 'f', None, ['-f', 'foo_value'], 'foo_value'),
('str', True, None, 'f', 'alt', ['--alt', 'foo_value'], 'foo_value'),
('int', True, None, 'f', None, ['-f', '1'], 1),
('long', True, None, 'f', None, ['-f', '1'], long(1)),
('float', True, None, 'f', None, ['-f', '1.23'], 1.23),
('complex', True, None, 'f', None, ['-f', '2j'], 2j),
])
def test_add_argument(simple_item, type, required,
default, short, args, expected):
def test_add_argument(
simple_item,
type,
required,
default,
short,
name,
args,
expected
):
simple_item.item_type = type
simple_item.required = required
simple_item.default = default
simple_item.cli_short_name = short
simple_item.cli_name = name
parser = ArgumentParser()
simple_item.add_argument(parser)
values = vars(parser.parse_args(args))
assert values[simple_item.name] == expected


@pytest.mark.parametrize('default,short_name,args,expected', [
(True, None, ['--no-my-bool'], False),
(False, None, ['--my-bool'], True),
(None, None, ['--my-bool'], True),
(None, None, ['--no-my-bool'], False),
(None, 'b', ['--no-b'], False),
(None, 'b', ['--b'], True),
(True, None, [], None),
(False, None, [], None),
@pytest.mark.parametrize('default,short_name,cli_name,args,expected', [
(True, None, None, ['--no-my-bool'], False),
(False, None, None, ['--my-bool'], True),
(None, None, None, ['--my-bool'], True),
(None, None, None, ['--no-my-bool'], False),
(None, None, 'alt', ['--no-alt'], False),
(None, None, 'alt', ['--alt'], True),
(None, 'b', None, ['--no-b'], False),
(None, 'b', None, ['--b'], True),
(True, None, None, [], None),
(False, None, None, [], None),
])
def test_add_bool_argument(bool_item, default, short_name, args, expected):
def test_add_bool_argument(
bool_item, default, short_name, cli_name, args, expected
):
bool_item.default = default
bool_item.cli_short_name = short_name
bool_item.cli_name = cli_name
parser = ArgumentParser()
bool_item.add_argument(parser)
values = vars(parser.parse_args(args))
Expand Down
33 changes: 22 additions & 11 deletions yapconf/items.py
Expand Up @@ -198,7 +198,8 @@ def __init__(
alt_env_names=None,
long_description=None,
validator=None,
fallback=None
fallback=None,
cli_name=None
):

self.name = name
Expand All @@ -223,6 +224,7 @@ def __init__(
self.choices = choices
self.validator = validator
self.fallback = fallback
self.cli_name = cli_name

if self.prefix:
self.fq_name = self.separator.join([self.prefix, self.name])
Expand Down Expand Up @@ -560,10 +562,11 @@ def _get_argparse_choices(self):

def _get_argparse_names(self, prefix_chars):
cli_prefix = self._format_prefix_for_cli(prefix_chars)
name = self.cli_name or self.name
if self.format_cli:
cli_name = yapconf.change_case(self.name, prefix_chars)
cli_name = yapconf.change_case(name, prefix_chars)
else:
cli_name = self.name
cli_name = name
if self.cli_short_name:
return ["{0}{1}".format(cli_prefix, cli_name),
"{0}{1}".format(prefix_chars, self.cli_short_name)]
Expand Down Expand Up @@ -622,7 +625,8 @@ def __init__(
alt_env_names=None,
long_description=None,
validator=None,
fallback=None
fallback=None,
cli_name=None
):
super(YapconfBoolItem, self).__init__(
name,
Expand All @@ -648,7 +652,8 @@ def __init__(
alt_env_names,
long_description,
validator,
fallback
fallback,
cli_name
)

def add_argument(self, parser, bootstrap=False):
Expand Down Expand Up @@ -716,10 +721,12 @@ def _get_argparse_action(self, parent_action=True):

def _get_argparse_names(self, prefix_chars):
cli_prefix = self._format_prefix_for_cli(prefix_chars)
name = self.cli_name or self.name

if self.format_cli:
cli_name = yapconf.change_case(self.name, prefix_chars)
cli_name = yapconf.change_case(name, prefix_chars)
else:
cli_name = self.name
cli_name = name

if self.default:
full_prefix = "{0}no{1}".format(cli_prefix, prefix_chars)
Expand Down Expand Up @@ -779,7 +786,8 @@ def __init__(
alt_env_names=None,
long_description=None,
validator=None,
fallback=None
fallback=None,
cli_name=None
):

super(YapconfListItem, self).__init__(
Expand All @@ -806,7 +814,8 @@ def __init__(
alt_env_names,
long_description,
validator,
fallback
fallback,
cli_name
)

if len(self.children) != 1:
Expand Down Expand Up @@ -936,7 +945,8 @@ def __init__(
alt_env_names=None,
long_description=None,
validator=None,
fallback=None
fallback=None,
cli_name=None
):

super(YapconfDictItem, self).__init__(
Expand All @@ -963,7 +973,8 @@ def __init__(
alt_env_names,
long_description,
validator,
fallback
fallback,
cli_name
)

if self.choices is not None:
Expand Down

0 comments on commit 78340e6

Please sign in to comment.