tparseopt.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. discard """
  2. output: '''
  3. parseopt
  4. first round
  5. kind: cmdLongOption key:val -- left:
  6. second round
  7. kind: cmdLongOption key:val -- left:
  8. kind: cmdLongOption key:val -- debug:3
  9. kind: cmdShortOption key:val -- l:4
  10. kind: cmdShortOption key:val -- r:2
  11. cmdLongOption foo
  12. cmdLongOption path
  13. parseoptNoVal
  14. kind: cmdLongOption key:val -- left:
  15. kind: cmdLongOption key:val -- debug:3
  16. kind: cmdShortOption key:val -- l:
  17. kind: cmdShortOption key:val -- r:2
  18. kind: cmdLongOption key:val -- debug:2
  19. kind: cmdLongOption key:val -- debug:1
  20. kind: cmdShortOption key:val -- r:1
  21. kind: cmdShortOption key:val -- r:0
  22. kind: cmdShortOption key:val -- l:
  23. kind: cmdShortOption key:val -- r:4
  24. kind: cmdLongOption key:val -- debug:
  25. cmdShortOption key: v value: ''
  26. cmdArgument key: ABC value: ''
  27. cmdShortOption key: v value: 'ABC'
  28. cmdShortOption key: v value: ''
  29. cmdArgument key: ABC value: ''
  30. cmdShortOption key: v value: ''
  31. cmdArgument key: ABC value: ''
  32. cmdShortOption key: j value: '4'
  33. cmdArgument key: ok value: ''
  34. '''
  35. joinable: false
  36. """
  37. when defined(testament_tparseopt):
  38. import os
  39. proc main() =
  40. let args = commandLineParams()
  41. echo args
  42. for i, ai in args:
  43. echo "arg ", i, " ai.len:", ai.len, " :{", ai, "}"
  44. main()
  45. else:
  46. from parseopt import nil
  47. block:
  48. echo "parseopt"
  49. for kind, key, val in parseopt.getopt():
  50. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  51. # pass custom cmdline arguments
  52. echo "first round"
  53. var argv = "--left --debug:3 -l=4 -r:2"
  54. var p = parseopt.initOptParser(argv)
  55. for kind, key, val in parseopt.getopt(p):
  56. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  57. break
  58. # reset getopt iterator and check arguments are returned correctly.
  59. echo "second round"
  60. for kind, key, val in parseopt.getopt(p):
  61. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  62. # bug #9619
  63. var x = parseopt.initOptParser(@["--foo:", "--path"],
  64. allowWhitespaceAfterColon = false)
  65. for kind, key, val in parseopt.getopt(x):
  66. echo kind, " ", key
  67. block:
  68. echo "parseoptNoVal"
  69. # test NoVal mode with custom cmdline arguments
  70. var argv = "--left --debug:3 -l -r:2 --debug 2 --debug=1 -r1 -r=0 -lr4 --debug:"
  71. var p = parseopt.initOptParser(argv,
  72. shortNoVal = {'l'}, longNoVal = @["left"])
  73. for kind, key, val in parseopt.getopt(p):
  74. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  75. import osproc, os, strutils
  76. from stdtest/specialpaths import buildDir
  77. import stdtest/unittest_light
  78. block: # fix #9951
  79. template runTest(parseoptCustom) =
  80. var p = parseoptCustom.initOptParser(@["echo \"quoted\""])
  81. let expected = when defined(windows):
  82. """"echo \"quoted\"""""
  83. else:
  84. """'echo "quoted"'"""
  85. assertEquals parseoptCustom.cmdLineRest(p), expected
  86. doAssert "a5'b" == "a5\'b"
  87. let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
  88. var p2 = parseoptCustom.initOptParser(args)
  89. let expected2 = when defined(windows):
  90. """a1b "a2 b" "" a4\"b a5'b a6\b a7'b"""
  91. else:
  92. """a1b 'a2 b' '' 'a4"b' 'a5'"'"'b' 'a6\b' 'a7'"'"'b'"""
  93. doAssert "a5'b" == "a5\'b"
  94. assertEquals parseoptCustom.cmdLineRest(p2), expected2
  95. runTest(parseopt)
  96. block: # fix #9842
  97. let exe = buildDir / "D20190112T145450".addFileExt(ExeExt)
  98. defer:
  99. when not defined(windows):
  100. # workaround #10359 ; innocuous to skip since we're saving under `buildDir`
  101. removeFile exe
  102. let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
  103. let cmd = "$# c -r --verbosity:0 -o:$# -d:testament_tparseopt $# $#" %
  104. [getCurrentCompilerExe(), exe, currentSourcePath(),
  105. args.quoteShellCommand]
  106. var ret = execCmdEx(cmd, options = {})
  107. if ret.exitCode != 0:
  108. # before bug fix, running cmd would show:
  109. # sh: -c: line 0: unexpected EOF while looking for matching `"'\n
  110. echo "exitCode: ", ret.exitCode, " cmd:", cmd
  111. doAssert false
  112. stripLineEnd(ret.output)
  113. assertEquals ret.output,
  114. """
  115. @["a1b", "a2 b", "", "a4\"b", "a5\'b", "a6\\b", "a7\'b"]
  116. arg 0 ai.len:3 :{a1b}
  117. arg 1 ai.len:4 :{a2 b}
  118. arg 2 ai.len:0 :{}
  119. arg 3 ai.len:4 :{a4"b}
  120. arg 4 ai.len:4 :{a5'b}
  121. arg 5 ai.len:4 :{a6\b}
  122. arg 6 ai.len:4 :{a7'b}"""
  123. block:
  124. let args = @["-v", "ABC"]
  125. var p = parseopt.initOptParser(args, shortnoVal = {'n'}, longnoVal = @["novalue"])
  126. for kind, key, val in parseopt.getopt(p):
  127. echo kind," key: ", key, " value: '", val, "'"
  128. var r = parseopt.initOptParser(@["-v ABC"], shortnoVal = {'n'}, longnoVal = @["novalue"])
  129. for kind, key, val in parseopt.getopt(r):
  130. echo kind," key: ", key, " value: '", val, "'"
  131. var s = parseopt.initOptParser("-v ABC", shortnoVal = {'v'}, longnoVal = @["novalue"])
  132. for kind, key, val in parseopt.getopt(s):
  133. echo kind," key: ", key, " value: '", val, "'"
  134. var m = parseopt.initOptParser("-v ABC", shortnoVal = {'n'}, longnoVal = @["novalue"])
  135. for kind, key, val in parseopt.getopt(m):
  136. echo kind," key: ", key, " value: '", val, "'"
  137. var n = parseopt.initOptParser("-j4 ok", shortnoVal = {'n'}, longnoVal = @["novalue"])
  138. for kind, key, val in parseopt.getopt(n):
  139. echo kind," key: ", key, " value: '", val, "'"