Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ./args.py foo bar -o baz -c qaz
- input: qaz
- output: baz
- parser_args: ['foo', 'bar']
- ./args.py foo bar -c -o baz qaz
- input: qaz
- output: baz
- parser_args: ['foo', 'bar']
- #!/usr/bin/env python
- import sys
- def help():
- print """
- This tool takes command line arguments in a way compatible with gcc. This is
- so that it can be compatible with ccache.
- Pass with -c the path to the rtdv file.
- Pass with -o the output path to the json file.
- All other args are forwarded to libclang.
- To accomodate ccache, the following syntax is also allowed:
- -c -o output input
- """
- def parse_cl_args(argv):
- input = False
- output = False
- parser_args = []
- i = 1
- while i < len(argv):
- # When we see -c, go into a mode where next parameter is the source file,
- # except that -o and its argument are allowed to appear inbetween
- if argv[i] == '-c':
- i+=1
- # Consume any -o immediately after -c
- while argv[i] == '-o':
- if output:
- raise Exception("Too many output files specified: " + output + " , " + argv[i+1])
- output = argv[i+1]
- i+=2
- # Consume -c parameter as input file
- if input:
- raise Exception("Too many input files specified: " + input + " , " + argv[i])
- input = argv[i]
- i+=1
- elif argv[i] == '-o':
- if output:
- raise Exception("Too many output files specified: " + output + " , " + argv[i+1])
- output = argv[i+1]
- i+=2
- else:
- parser_args += [argv[i]]
- i+=1
- return (input, output, parser_args)
- def main():
- if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) <= 1:
- help()
- return
- (input, output, parser_args) = parse_cl_args(sys.argv)
- # if not input:
- # sys.stderr.write('Argv: ' + str(sys.argv) + 'n')
- # raise Exception("Must use -c flag to specify dv file to parse.")
- #
- # if not output:
- # sys.stderr.write('Argv: ' + str(sys.argv) + 'n')
- # raise Exception("Must use -o flag to specify output path for json")
- print 'input: ', input
- print 'output: ', output
- print 'parser_args: ', parser_args
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment