Coverage for src/hatch_ci/__main__.py: 0%

1 from __future__ import annotations

2

3 import argparse

4 import functools

5 import logging

6 import sys

7 from typing import Any

8

9 from . import tools

10

11 log = logging.getLogger(__name__)

12

13

14 def parse_args(args: str | None = None, testmode: bool = False) -> dict[str, Any]:

15 """parses args from the command line

16

17 Args:

18 args: command line arguments or None to pull from sys.argv

19 testmode: internal flag, if set will not SystemExit but will

20 raises tools.AbortExecution

21 """

22

23 class F(

24 argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter

25 ):

26 pass

27

28 parser = argparse.ArgumentParser(formatter_class=F, description=__doc__)

29

30 parser.add_argument("-n", "--dry-run", dest="dryrun", action="store_true")

31 parser.add_argument("-v", "--verbose", action="store_true")

32

33 options = parser.parse_args(args)

34

35 def error(message, explain="", hint="", parser=None, testmode=False):

36 out = []

37 if parser:

38 out.extend(tools.indent(parser.format_usage()).split("\n"))

39 if message:

40 out.extend(tools.indent(message).split("\n"))

41 if explain:

42 out.append("reason:")

43 out.extend(tools.indent(explain).split("\n"))

44 if hint:

45 out.append("hint:")

46 out.extend(tools.indent(hint).split("\n"))

47

48 if testmode:

49 raise tools.AbortExecution(message, explain, hint)

50 else:

51 print() # noqa: T201

52 print("\n".join(out), file=sys.stderr) # noqa: T201

53 raise SystemExit(2)

54

55 options.error = functools.partial(error, parser=parser, testmode=testmode)

56

57 logging.basicConfig(

58 format="%(levelname)s:%(name)s:(dry-run) %(message)s"

59 if options.dryrun

60 else "%(levelname)s:%(name)s:%(message)s",

61 level=logging.DEBUG if options.verbose else logging.INFO,

62 )

63

64 for d in [

65 "verbose",

66 ]:

67 delattr(options, d)

68 return options.__dict__

69

70

71 def run():

72 pass

73

74

75 def main():

76 run(**parse_args())

77

78

79 if __name__ == "__main__":

80 main()