Coverage for lice2/cli.py: 0%

1"""Define the command-line interface for the `lice` package."""

2

3 import argparse

4 import re

5 from datetime import datetime

6 from pathlib import Path

7

8 from lice2.constants import LANGS, LICENSES

9 from lice2.helpers import guess_organization

10

11

12 def get_args() -> argparse.Namespace:

13 """Set up the arg parsing and return it."""

14

15 def valid_year(string: str) -> str:

16 if not re.match(r"^\d{4}$", string):

17 message = "Must be a four-digit year"

18 raise argparse.ArgumentTypeError(message)

19 return string

20

21 parser = argparse.ArgumentParser(description="Generate a license")

22

23 parser.add_argument(

24 "license",

25 metavar="license",

26 nargs="?",

27 choices=LICENSES,

28 help=f"the license to generate, one of: {', '.join(LICENSES)}",

29 )

30 parser.add_argument(

31 "--header",

32 dest="header",

33 action="store_true",

34 help="generate source file header for specified license",

35 )

36 parser.add_argument(

37 "-o",

38 "--org",

39 dest="organization",

40 default=guess_organization(),

41 help='organization, defaults to .gitconfig or os.environ["USER"]',

42 )

43 parser.add_argument(

44 "-p",

45 "--proj",

46 dest="project",

47 default=Path.cwd().name,

48 help="name of project, defaults to name of current directory",

49 )

50 parser.add_argument(

51 "-t",

52 "--template",

53 dest="template_path",

54 help="path to license template file",

55 )

56 parser.add_argument(

57 "-y",

58 "--year",

59 dest="year",

60 type=valid_year,

61 default="%i" % datetime.now().date().year, # noqa: DTZ005

62 help="copyright year",

63 )

64 parser.add_argument(

65 "-l",

66 "--language",

67 dest="language",

68 help="format output for language source file, one of: "

69 f"{', '.join(LANGS.keys())} [default is not formatted (txt)]",

70 )

71 parser.add_argument(

72 "-f",

73 "--file",

74 dest="ofile",

75 default="stdout",

76 help="Name of the output source file (with -l, "

77 "extension can be ommitted)",

78 )

79 parser.add_argument(

80 "--vars",

81 dest="list_vars",

82 action="store_true",

83 help="list template variables for specified license",

84 )

85 parser.add_argument(

86 "--licenses",

87 dest="list_licenses",

88 action="store_true",

89 help="list available license templates and their parameters",

90 )

91 parser.add_argument(

92 "--languages",

93 dest="list_languages",

94 action="store_true",

95 help="list available source code formatting languages",

96 )

97

98 return parser.parse_args()