5
5
from argparse import ArgumentParser , FileType , RawDescriptionHelpFormatter
6
6
from traceback import format_exception
7
7
8
+ from . import __version__
8
9
from .lexer import lex as lex_file
9
10
from .parser import parse as parse_file
10
11
from .errors import NgxParserBaseException
@@ -70,12 +71,14 @@ def _dump_payload(obj, fp, indent):
70
71
fp .write (json .dumps (obj , ** kwargs ) + '\n ' )
71
72
72
73
73
- def parse (filename , out , indent = None , catch = None , tb_onerror = None ):
74
+ def parse (filename , out , indent = None , catch = None , tb_onerror = None , ignore = '' , single = False ):
75
+ ignore = ignore .split (',' ) if ignore else []
76
+
74
77
def callback (e ):
75
78
exc = sys .exc_info () + (10 ,)
76
79
return '' .join (format_exception (* exc )).rstrip ()
77
80
78
- kwargs = {'catch_errors' : catch }
81
+ kwargs = {'catch_errors' : catch , 'ignore' : ignore , 'single' : single }
79
82
if tb_onerror :
80
83
kwargs ['onerror' ] = callback
81
84
@@ -157,6 +160,7 @@ def parse_args(args=None):
157
160
description = 'various operations for nginx config files' ,
158
161
usage = '%(prog)s <command> [options]'
159
162
)
163
+ parser .add_argument ('-V' , '--version' , action = 'version' , version = '%(prog)s ' + __version__ )
160
164
subparsers = parser .add_subparsers (title = 'commands' )
161
165
162
166
def create_subparser (function , help ):
@@ -170,8 +174,10 @@ def create_subparser(function, help):
170
174
p .add_argument ('filename' , help = 'the nginx config file' )
171
175
p .add_argument ('-o' , '--out' , type = FileType ('w' ), default = '-' , help = 'write output to a file' )
172
176
p .add_argument ('-i' , '--indent' , type = int , metavar = 'NUM' , help = 'number of spaces to indent output' )
177
+ p .add_argument ('--ignore' , metavar = 'DIRECTIVES' , default = '' , help = 'ignore directives (comma-separated)' )
173
178
p .add_argument ('--no-catch' , action = 'store_false' , dest = 'catch' , help = 'only collect first error in file' )
174
179
p .add_argument ('--tb-onerror' , action = 'store_true' , help = 'include tracebacks in config errors' )
180
+ p .add_argument ('--single-file' , action = 'store_true' , dest = 'single' , help = 'do not include other config files' )
175
181
176
182
p = create_subparser (lex , 'lexes tokens from an nginx config file' )
177
183
p .add_argument ('filename' , help = 'the nginx config file' )
@@ -201,7 +207,7 @@ def help(command):
201
207
202
208
parsed = parser .parse_args (args = args )
203
209
204
- # this addresses a bug that was added to argparse in Python 3.3
210
+ # this addresses a bug that was added to argparse in Python 3.3
205
211
if not parsed .__dict__ :
206
212
parser .error ('too few arguments' )
207
213
0 commit comments