Skip to content

Commit

Permalink
cli: Prettify CL help messages, relates to tomazc#86
Browse files Browse the repository at this point in the history
  • Loading branch information
JureZmrzlikar committed Feb 15, 2017
1 parent c4bca3b commit f869965
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
3 changes: 1 addition & 2 deletions iCount/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
A number of analyses are included in iCount that provide insights into the properties of
protein-RNA interaction.
.. _iCLIP:
http://www.ulelab.info
.. _iCLIP: http://www.ulelab.info
"""

Expand Down
38 changes: 21 additions & 17 deletions iCount/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ def _list_str(string, separator=','):
PARAMETERS = {}


def remove_comments(description):
def _remove_comments(description_in):
"""Remove reStructuredText comment lines."""
description = description.splitlines()
if len(description) > 0:
if description[0].startswith('..'):
description = description[1:]
while description and description[0].strip() == '':
description.pop(0)
description = []
for line in description_in.splitlines():
if not line.startswith('..'):
description.append(line)

# Remove first/last line if it is empty:
while description[0] == '':
description = description[1:]
while description[-1] == '':
description = description[:-1]

return '\n'.join(description)

Expand Down Expand Up @@ -248,7 +252,7 @@ def make_parser_from_function(function, subparsers, module=None, only_func=False
name = module.__name__.split('.')[-1]
# Description is determined from module docstring:
description = inspect.getdoc(module)
description = remove_comments(description)
description = _remove_comments(description)
description = '\n'.join(description.split('\n')[3:])
elif only_func:
# If only_func=True than name of the command equals function name
Expand All @@ -257,13 +261,13 @@ def make_parser_from_function(function, subparsers, module=None, only_func=False
desc = inspect.getdoc(function).split('\n')
idx = [i for i, d in enumerate(desc) if d == 'Parameters' or d == 'Returns'][0]
description = '\n'.join(desc[:idx])
description = remove_comments(description)
description = _remove_comments(description)
else:
# Command name is determined from module name:
name = inspect.getmodule(function).__name__.split('.')[-1]
# Description is determined from module docstring:
description = inspect.getdoc(inspect.getmodule(function))
description = remove_comments(description)
description = _remove_comments(description)
description = '\n'.join(description.split('\n')[3:])

if not description:
Expand All @@ -275,7 +279,7 @@ def make_parser_from_function(function, subparsers, module=None, only_func=False
name,
description=description,
help=short_description,
formatter_class=argparse.RawTextHelpFormatter # ArgumentDefaultsHelpFormatter
formatter_class=argparse.RawDescriptionHelpFormatter # ArgumentDefaultsHelpFormatter
)

# Add each of the function parameter as CLI argument:
Expand All @@ -293,14 +297,14 @@ def make_parser_from_function(function, subparsers, module=None, only_func=False

parser.add_argument('-S', '--stdout_log', default=logging.INFO, type=int, metavar='',
help='Threshold value (0-50) for logging to stdout. If 0,'
' logging to stdout if turned OFF.')
' logging to stdout if turned OFF. (default: {})'.format(logging.INFO))
parser.add_argument('-F', '--file_log', default=0, type=int, metavar='',
help='Threshold value (0-50) for logging to file. If 0,'
' logging to file if turned OFF.')
' logging to file if turned OFF. (default: {})'.format(0))
parser.add_argument('-P', '--file_logpath', default=None, type=str, metavar='',
help="Path to log file.")
help="Path to log file. (default: {})".format(None))
parser.add_argument('-M', '--results_file', default=None, type=argparse.FileType('wt'),
metavar='', help="File into which to store Metrics.")
metavar='', help="File to store Metrics. (default: {})".format(None))

# Connect parser with function to execute:
parser.set_defaults(func=function)
Expand All @@ -320,8 +324,8 @@ def main():
#####################

root_parser = argparse.ArgumentParser(
description=remove_comments(inspect.getdoc(iCount)).split('\n..')[0],
formatter_class=argparse.RawTextHelpFormatter # ArgumentDefaultsHelpFormatter,
description=_remove_comments(inspect.getdoc(iCount)),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
# Parser (general) arguments
root_parser.add_argument('-v', '--version', action='version',
Expand Down

0 comments on commit f869965

Please sign in to comment.