Skip to content

Commit

Permalink
Merge branch 'release/0.1.17'
Browse files Browse the repository at this point in the history
  • Loading branch information
dpryan79 committed Feb 24, 2017
2 parents 6ba32da + e7672e3 commit 4dd6b91
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ nosetests.xml
.env/

docs/_build/
.cache/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- "3.3"
- "3.4"
- "3.5"
- "3.6"
install:
- pip install .
- pip install flake8
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ http://sphinx-argparse.readthedocs.org/en/latest/
Changelog & contributors
========================

0.1.17
------

- Fixed handling of argument groups (this was bug #49). Thanks to @croth1 for reporting this bug. Note that now position arguments (also known as required arguments) within argument groups are now also handled correctly.

0.1.16
------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='sphinx-argparse',
version='0.1.16',
version='0.1.17',
packages=[
'sphinxarg',
],
Expand Down
2 changes: 1 addition & 1 deletion sphinxarg/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def print_arg_list(data, nested_content):

def print_opt_list(data, nested_content):
definitions = map_nested_definitions(nested_content)
items = []
nodes_list = [] # dictionary to hold the group options, the group title is used as the key
if 'action_groups' in data:
for action_group in data['action_groups']:
items = []
if 'options' in action_group:
for opt in action_group['options']:
names = []
Expand Down
117 changes: 57 additions & 60 deletions sphinxarg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,70 +60,50 @@ def parse_parser(parser, data=None, **kwargs):
_try_add_parser_attribute(data, parser, 'description')
_try_add_parser_attribute(data, parser, 'epilog')
for action in parser._get_positional_actions():
if isinstance(action, _HelpAction):
if not isinstance(action, _SubParsersAction):
continue
if isinstance(action, _SubParsersAction):
helps = {}
for item in action._choices_actions:
helps[item.dest] = item.help

# commands which share an existing parser are an alias,
# don't duplicate docs
subsection_alias = {}
subsection_alias_names = set()
for name, subaction in action._name_parser_map.items():
if subaction not in subsection_alias:
subsection_alias[subaction] = []
else:
subsection_alias[subaction].append(name)
subsection_alias_names.add(name)

for name, subaction in action._name_parser_map.items():
if name in subsection_alias_names:
continue
subalias = subsection_alias[subaction]
subaction.prog = '%s %s' % (parser.prog, name)
subdata = {
'name': name if not subalias else '%s (%s)' % (name, ', '.join(subalias)),
'help': helps.get(name, ''),
'usage': subaction.format_usage().strip(),
'bare_usage': _format_usage_without_prefix(subaction),
}
parse_parser(subaction, subdata, **kwargs)
data.setdefault('children', []).append(subdata)
continue
if 'args' not in data:
data['args'] = []

# Fill in things like %(prog)s in the help section.
# Note that if any keyword is missing, then nothing is filled in.
formatDict = dict(vars(action), prog=data.get('prog', ''))
helpStr = action.help or '' # Ensure we don't print None
try:
helpStr = helpStr % formatDict
except:
pass

arg = {
'name': action.dest,
'help': helpStr,
'metavar': action.metavar
}
if action.choices:
arg['choices'] = action.choices
data['args'].append(arg)
show_defaults = (
('skip_default_values' not in kwargs) or
(kwargs['skip_default_values'] is False))
helps = {}
for item in action._choices_actions:
helps[item.dest] = item.help

# commands which share an existing parser are an alias,
# don't duplicate docs
subsection_alias = {}
subsection_alias_names = set()
for name, subaction in action._name_parser_map.items():
if subaction not in subsection_alias:
subsection_alias[subaction] = []
else:
subsection_alias[subaction].append(name)
subsection_alias_names.add(name)

for name, subaction in action._name_parser_map.items():
if name in subsection_alias_names:
continue
subalias = subsection_alias[subaction]
subaction.prog = '%s %s' % (parser.prog, name)
subdata = {
'name': name if not subalias else '%s (%s)' % (name, ', '.join(subalias)),
'help': helps.get(name, ''),
'usage': subaction.format_usage().strip(),
'bare_usage': _format_usage_without_prefix(subaction),
}
parse_parser(subaction, subdata, **kwargs)
data.setdefault('children', []).append(subdata)

show_defaults = True
if 'skip_default_values' in kwargs and kwargs['skip_default_values'] is True:
show_defaults = False
show_defaults_const = show_defaults
if 'skip_default_const_values' in kwargs and kwargs['skip_default_const_values'] is True:
show_defaults_const = False

# argparse stores the different groups as a lint in parser._action_groups
# the first element of the list are the positional arguments, hence the
# parser._actions_groups[1:]
# argparse stores the different groups as a list in parser._action_groups
# the first element of the list holds the positional arguments, the
# second the option arguments not in groups, and subsequent elements
# argument groups with positional and optional parameters
action_groups = []
for action_group in parser._action_groups[1:]:
for action_group in parser._action_groups:
options_list = []
for action in action_group._group_actions:
if isinstance(action, _HelpAction):
Expand All @@ -141,15 +121,26 @@ def parse_parser(parser, data=None, **kwargs):
except:
pass

# Options have the option_strings set, positional arguments don't
name = action.option_strings
if name == []:
if action.metavar is None:
name = [action.dest]
else:
name = [action.metavar]
# Skip lines for subcommands
if name == ['==SUPPRESS==']:
continue

if isinstance(action, _StoreConstAction):
option = {
'name': action.option_strings,
'name': name,
'default': action.default if show_defaults_const else '==SUPPRESS==',
'help': helpStr
}
else:
option = {
'name': action.option_strings,
'name': name,
'default': action.default if show_defaults else '==SUPPRESS==',
'help': helpStr
}
Expand All @@ -161,6 +152,12 @@ def parse_parser(parser, data=None, **kwargs):
if len(options_list) == 0:
continue

# Upper case "Positional Arguments" and "Optional Arguments" titles
if action_group.title == 'optional arguments':
action_group.title = 'Optional Arguments'
if action_group.title == 'positional arguments':
action_group.title = 'Required Arguments'

group = {'title': action_group.title,
'description': action_group.description,
'options': options_list}
Expand Down
Loading

0 comments on commit 4dd6b91

Please sign in to comment.