File: cli.py

package info (click to toggle)
argparse-manpage 4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 536 kB
  • sloc: python: 2,755; makefile: 47; sh: 18
file content (101 lines) | stat: -rw-r--r-- 3,589 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright (C) 2017 Red Hat, Inc.

# Python 2.7 hack.  Without this, 'from build_manpages.build_manpage' attempts
# to import 'build_manpages.build_manpages.build_manpage' because of our
# unfortunate file naming.
from __future__ import absolute_import

import argparse

from argparse_manpage.tooling import get_parser, write_to_filename
from argparse_manpage.manpage import MANPAGE_DATA_ATTRS, Manpage


description = """
Build manual page from Python's argparse.ArgumentParser object.
""".strip()

ap = argparse.ArgumentParser(
    prog='argparse-manpage',
    description=description,
)

src_group = ap.add_mutually_exclusive_group(required=True)
src_group.add_argument(
    "--module",
    help="search the OBJECT/FUNCTION in MODULE"
)

src_group.add_argument(
    "--pyfile",
    help="search the OBJECT/FUNCTION in FILE"
)

obj_group = ap.add_mutually_exclusive_group(required=True)
obj_group.add_argument(
    "--function",
    help="call FUNCTION from MODULE/FILE to obtain ArgumentParser object",
)
obj_group.add_argument(
    "--object",
    help="obtain ArgumentParser OBJECT from FUNCTION (to get argparse object) from MODULE or FILE",
)


ap.add_argument("--project-name", help="Name of the project the documented program is part of.")
ap.add_argument("--prog", help="Substitutes %%prog in ArgumentParser's usage.")
ap.add_argument("--version", help=(
    "Version of the program, will be visible in the "
    "manual page footer."))
ap.add_argument("--description", metavar="TEXT", help=(
    "description of the program, used in the NAME section, after the "
    "leading 'name - ' part, see man (7) man-pages for more info"))
ap.add_argument("--long-description", metavar="TEXT", help=argparse.SUPPRESS)
ap.add_argument("--author", action="append", dest="authors", metavar="[AUTHOR]",
                help="Author of the program. Can be specified multiple times.")
ap.add_argument("--author-email", action="append", dest="authors",
                help=argparse.SUPPRESS)
ap.add_argument("--url", help="Link to project's homepage")
ap.add_argument("--format", default="pretty", choices=("pretty", "single-commands-section"),
                help="Format of the generated man page. Defaults to 'pretty'.")
ap.add_argument("--output", dest='outfile', default='-',
                help="Output file. Defaults to stdout.")
ap.add_argument("--manual-section", help=(
    "Section of the manual, by default 1.  See man (7) man-pages for more "
    "info about existing sections."))
ap.add_argument("--manual-title", help=(
    "The title of the manual, by default \"Generated Python Manual\". "
    "See man (7) man-pages for more instructions."))
ap.add_argument("--include", metavar="FILE", help=(
    "File that contains extra material for the man page."))
ap.add_argument("--manfile", metavar="FILE", help=(
    "File containing a complete man page."))


def args_to_manpage_data(args):
    data = {}
    for attr in MANPAGE_DATA_ATTRS:
        value = getattr(args, attr)
        data[attr] = value
    return data


def main():
    args = ap.parse_args()

    import_type = 'pyfile'
    import_from = args.pyfile
    if args.module:
        import_type = 'module'
        import_from = args.module

    obj_type = 'object'
    obj_name = args.object
    if args.function:
        obj_type = 'function'
        obj_name = args.function

    parser = get_parser(import_type, import_from, obj_name, obj_type, prog=args.prog)
    data = args_to_manpage_data(args)
    manpage = Manpage(parser, format=args.format, _data=data)
    write_to_filename(str(manpage), args.outfile)