11from argparse import ArgumentParser , Namespace
2- from typing import List , Optional , Tuple
2+ from typing import Dict , List , Optional , Tuple , Union
33
44from naughtty .constants import DEFAULT_CHARACTER_PIXELS , DEFAULT_TERMINAL_SIZE
55from naughtty .naughtty import NaughTTY
66from naughtty .version import get_version
77
88
9+ def make_namespace (cli_args : List [str ]) -> Namespace :
10+ """Reads `cli_args` into a `Namespace`."""
11+
12+ # We need to perform our own strict left-to-right reading of the arguments
13+ # because `ArgumentParser` can't tell if any "--help" or "--version" is
14+ # intended for us or the child command.
15+
16+ wip : Dict [str , Union [bool , str , List [str ]]] = {}
17+ name : Optional [str ] = None
18+
19+ for index , arg in enumerate (cli_args ):
20+ if arg .startswith ("--" ):
21+ name = arg [2 :].replace ("-" , "_" )
22+ wip [name ] = True
23+ else :
24+ if name :
25+ wip [name ] = arg
26+ name = None
27+ else :
28+ wip ["command" ] = cli_args [index :]
29+ break
30+
31+ return Namespace (** wip )
32+
33+
934def make_naughtty (ns : Namespace ) -> NaughTTY :
1035 """Makes a `NaughTTY` instance based on the given command line arguments."""
1136
1237 character_pixels : Optional [Tuple [int , int ]] = None
1338
14- if ns . character_pixels :
39+ if " character_pixels" in ns :
1540 parts = str (ns .character_pixels ).split ("," )
1641 character_pixels = (int (parts [0 ]), int (parts [1 ]))
1742
1843 return NaughTTY (
19- columns = int (ns .columns ) if ns . columns else None ,
44+ columns = int (ns .columns ) if " columns" in ns else None ,
2045 command = ns .command ,
2146 character_pixels = character_pixels ,
22- lines = int (ns .lines ) if ns . lines else None ,
47+ lines = int (ns .lines ) if " lines" in ns else None ,
2348 )
2449
2550
26- def make_response (cli_args : Optional [ List [str ]] = None ) -> str :
51+ def make_response (cli_args : List [str ]) -> str :
2752 """Makes a response to the given command line arguments."""
2853
2954 parser = ArgumentParser (
30- # We don't want ArgumentParser to pick up on "--help" in the child
31- # command's arguments:
32- add_help = False ,
3355 description = "Executes a shell command in a pseudo-terminal and prints its output to stdout." ,
3456 epilog = "Made with love by Cariad Eccleston: https://github.com/cariad/naughtty" ,
3557 )
@@ -47,8 +69,6 @@ def make_response(cli_args: Optional[List[str]] = None) -> str:
4769 help = f"columns (default=system default or { DEFAULT_TERMINAL_SIZE [0 ]} )" ,
4870 )
4971
50- parser .add_argument ("--help" , action = "store_true" , help = "print this help" )
51-
5272 parser .add_argument (
5373 "--lines" ,
5474 help = f"lines (default=system default or { DEFAULT_TERMINAL_SIZE [1 ]} )" ,
@@ -60,14 +80,12 @@ def make_response(cli_args: Optional[List[str]] = None) -> str:
6080 help = "print the version" ,
6181 )
6282
63- args = parser . parse_args (cli_args )
83+ args = make_namespace (cli_args )
6484
65- if args . version :
85+ if " version" in args :
6686 return get_version ()
6787
68- # If we discover "--help" AND a command then that "--help" is intended for
69- # the command and not us:
70- if not args .command or (args .help and not args .command ):
88+ if "command" not in args or "help" in args :
7189 return parser .format_help ()
7290
7391 n = make_naughtty (args )
0 commit comments