Guest User

Untitled

a guest
Nov 20th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. ./args.py foo bar -o baz -c qaz
  2. input: qaz
  3. output: baz
  4. parser_args: ['foo', 'bar']
  5.  
  6. ./args.py foo bar -c -o baz qaz
  7. input: qaz
  8. output: baz
  9. parser_args: ['foo', 'bar']
  10.  
  11. #!/usr/bin/env python
  12.  
  13. import sys
  14.  
  15. def help():
  16. print """
  17. This tool takes command line arguments in a way compatible with gcc. This is
  18. so that it can be compatible with ccache.
  19.  
  20. Pass with -c the path to the rtdv file.
  21. Pass with -o the output path to the json file.
  22. All other args are forwarded to libclang.
  23.  
  24.  
  25. To accomodate ccache, the following syntax is also allowed:
  26. -c -o output input
  27. """
  28.  
  29.  
  30. def parse_cl_args(argv):
  31. input = False
  32. output = False
  33. parser_args = []
  34.  
  35. i = 1
  36. while i < len(argv):
  37. # When we see -c, go into a mode where next parameter is the source file,
  38. # except that -o and its argument are allowed to appear inbetween
  39. if argv[i] == '-c':
  40. i+=1
  41. # Consume any -o immediately after -c
  42. while argv[i] == '-o':
  43. if output:
  44. raise Exception("Too many output files specified: " + output + " , " + argv[i+1])
  45. output = argv[i+1]
  46. i+=2
  47.  
  48. # Consume -c parameter as input file
  49. if input:
  50. raise Exception("Too many input files specified: " + input + " , " + argv[i])
  51. input = argv[i]
  52. i+=1
  53. elif argv[i] == '-o':
  54. if output:
  55. raise Exception("Too many output files specified: " + output + " , " + argv[i+1])
  56. output = argv[i+1]
  57. i+=2
  58. else:
  59. parser_args += [argv[i]]
  60. i+=1
  61.  
  62. return (input, output, parser_args)
  63.  
  64. def main():
  65. if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) <= 1:
  66. help()
  67. return
  68.  
  69. (input, output, parser_args) = parse_cl_args(sys.argv)
  70.  
  71. # if not input:
  72. # sys.stderr.write('Argv: ' + str(sys.argv) + 'n')
  73. # raise Exception("Must use -c flag to specify dv file to parse.")
  74. #
  75. # if not output:
  76. # sys.stderr.write('Argv: ' + str(sys.argv) + 'n')
  77. # raise Exception("Must use -o flag to specify output path for json")
  78.  
  79. print 'input: ', input
  80. print 'output: ', output
  81. print 'parser_args: ', parser_args
  82.  
  83. if __name__ == '__main__':
  84. main()
Add Comment
Please, Sign In to add comment