Skip to content

Commit

Permalink
Compatibility with argparse-manpage
Browse files Browse the repository at this point in the history
* Refactor construction of `argparse.ArgumentParser` into a `get_argparser()`
  function in each binary.
* Set `prog` property (the default uses `sys.arg[0]` which breaks with
  `argparse-manpage`, arguably this is an `argparse-manpage` bug).
* Set the `man_short_description` property based on the first line of the
  description. This is used as a summary in the `NAME` section of the man page.
  • Loading branch information
ijc committed Jan 8, 2023
1 parent 6028972 commit 8e588fd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
19 changes: 14 additions & 5 deletions cmakelang/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,26 @@ def setup_argparser(arg_parser):
arg_parser.add_argument('infilepaths', nargs='*')


def get_argparser():
argparser = argparse.ArgumentParser(
prog='cmake-annotate',
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE_STRING)

setattr(argparser, 'man_short_description', __doc__.strip().splitlines()[0])

setup_argparser(argparser)
return argparser


def main():
"""Parse arguments, open files, start work."""

# set up main logger, which logs everything. We'll leave this one logging
# to the console
logging.basicConfig(level=logging.INFO)
arg_parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE_STRING)
setup_argparser(arg_parser)
arg_parser = get_argparser()
args = arg_parser.parse_args()

assert (len(args.infilepaths) == 1
Expand Down
13 changes: 10 additions & 3 deletions cmakelang/format/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,22 @@ def onefile_main(infile_path, args, argparse_dict):
shutil.move(tempfile_path, infile_path)


def inner_main():
"""Parse arguments, open files, start work."""

def get_argparser():
arg_parser = argparse.ArgumentParser(
prog='cmake-format',
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE_STRING)

setattr(arg_parser, 'man_short_description', __doc__.strip().splitlines()[0])

setup_argparser(arg_parser)
return arg_parser

def inner_main():
"""Parse arguments, open files, start work."""

arg_parser = get_argparser()
try:
import argcomplete
argcomplete.autocomplete(arg_parser)
Expand Down
23 changes: 15 additions & 8 deletions cmakelang/genparsers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
"""
Parse cmake listfiles, find function and macro declarations, and generate
parsers for them.
Parse cmake listfiles, find function and macro declarations, and generate parsers for them.
"""
from __future__ import print_function, unicode_literals

Expand Down Expand Up @@ -214,6 +213,19 @@ def setup_argparse(argparser):
argparser.add_argument('infilepaths', nargs='*')


def get_argparser():
argparser = argparse.ArgumentParser(
prog='cmake-genparsers',
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE_STRING)

setattr(argparser, 'man_short_description', __doc__.strip().splitlines()[0])

setup_argparse(argparser)
return argparser


USAGE_STRING = """
cmake-genparsers [-h] [-o OUTFILE_PATH] infilepath [infilepath ...]
"""
Expand All @@ -223,12 +235,7 @@ def main():
"""Parse arguments, open files, start work."""
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")

argparser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE_STRING)

setup_argparse(argparser)
argparser = get_argparser()
args = argparser.parse_args()
logging.getLogger().setLevel(getattr(logging, args.log_level.upper()))

Expand Down
19 changes: 13 additions & 6 deletions cmakelang/lint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,23 @@ def setup_argparse(argparser):
"""


def inner_main():
"""Parse arguments, open files, start work."""
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")

argparser = argparse.ArgumentParser(
def get_argparser():
arg_parser = argparse.ArgumentParser(
prog='cmake-lint',
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE_STRING)

setup_argparse(argparser)
setattr(arg_parser, 'man_short_description', __doc__.strip().splitlines()[0])

setup_argparse(arg_parser)
return arg_parser

def inner_main():
"""Parse arguments, open files, start work."""
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")

argparser = get_argparser()
try:
import argcomplete
argcomplete.autocomplete(argparser)
Expand Down

0 comments on commit 8e588fd

Please sign in to comment.