3
3
"""
4
4
5
5
from argparse import ArgumentParser , RawDescriptionHelpFormatter
6
- from contextlib import suppress
7
6
from datetime import datetime
8
7
9
8
from . import FILETYPE , __version__
10
9
from . import __name__ as NAME
11
10
11
+ try :
12
+ import shtab
13
+ except ImportError :
14
+ import _shtab as shtab
15
+
12
16
NAME = NAME .replace ("_" , "-" )
13
17
VERSION = rf"""{ NAME } { __version__ }
14
18
Copyright (C) { datetime .now ().year }
@@ -25,52 +29,49 @@ def get_parser():
25
29
epilog = EPILOG ,
26
30
formatter_class = RawDescriptionHelpFormatter ,
27
31
)
28
- with suppress (ImportError ):
29
- import shtab
30
-
31
- shtab .add_argument_to (parser )
32
+ shtab .add_argument_to (parser )
32
33
parser .add_argument ("--version" , version = VERSION , action = "version" )
33
34
parser .add_argument (
34
35
"--generate-schema" ,
35
36
choices = FILETYPE .__args__ , # type: ignore
36
37
help = "generate schema in an output format" ,
37
38
)
39
+ parser .add_argument (
40
+ "--output-format" ,
41
+ choices = ["json" , "yaml" , "toml" ],
42
+ default = "json" ,
43
+ help = "output format: %(default)s" ,
44
+ )
38
45
parser .add_argument (
39
46
"--indent" ,
40
47
type = int ,
41
48
default = 2 ,
42
- help = "generated json's indent" ,
49
+ help = "generated json, yaml's indent, ignored by toml: %(default)s" ,
50
+ )
51
+ parser .add_argument (
52
+ "--color" ,
53
+ choices = ["auto" , "always" , "never" ],
54
+ default = "auto" ,
55
+ help = "when to display color, default: %(default)s" ,
43
56
)
44
57
parser .add_argument (
45
58
"--check" ,
46
59
nargs = "*" ,
47
60
default = {},
48
61
help = "check file's errors and warnings" ,
49
- )
62
+ ). complete = shtab . FILE # type: ignore
50
63
parser .add_argument (
51
64
"--format" ,
52
65
nargs = "*" ,
53
66
default = {},
54
67
help = "format files" ,
55
- )
56
- parser .add_argument (
57
- "--color" ,
58
- choices = ["auto" , "always" , "never" ],
59
- default = "auto" ,
60
- help = "when to display color, default: %(default)s" ,
61
- )
68
+ ).complete = shtab .FILE # type: ignore
62
69
parser .add_argument (
63
70
"--convert" ,
64
71
nargs = "*" ,
65
72
default = {},
66
73
help = "convert files to output format" ,
67
- )
68
- parser .add_argument (
69
- "--output-format" ,
70
- choices = ["json" , "yaml" , "toml" ],
71
- default = "json" ,
72
- help = "output format: %(default)s" ,
73
- )
74
+ ).complete = shtab .FILE # type: ignore
74
75
return parser
75
76
76
77
@@ -92,10 +93,13 @@ def main():
92
93
if args .generate_schema :
93
94
from .misc import get_schema
94
95
96
+ kwargs = (
97
+ {"indent" : args .indent } if args .output_format != "toml" else {}
98
+ )
95
99
pprint (
96
100
get_schema (args .generate_schema ),
97
101
filetype = args .output_format ,
98
- indent = args . indent ,
102
+ ** kwargs ,
99
103
)
100
104
for file in args .convert :
101
105
pprint (
0 commit comments