tparseopt.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. '''
  26. joinable: false
  27. """
  28. when defined(testament_tparseopt):
  29. import os
  30. proc main() =
  31. let args = commandLineParams()
  32. echo args
  33. for i, ai in args:
  34. echo "arg ", i, " ai.len:", ai.len, " :{", ai, "}"
  35. main()
  36. else:
  37. from parseopt import nil
  38. block:
  39. echo "parseopt"
  40. for kind, key, val in parseopt.getopt():
  41. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  42. # pass custom cmdline arguments
  43. echo "first round"
  44. var argv = "--left --debug:3 -l=4 -r:2"
  45. var p = parseopt.initOptParser(argv)
  46. for kind, key, val in parseopt.getopt(p):
  47. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  48. break
  49. # reset getopt iterator and check arguments are returned correctly.
  50. echo "second round"
  51. for kind, key, val in parseopt.getopt(p):
  52. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  53. # bug #9619
  54. var x = parseopt.initOptParser(@["--foo:", "--path"],
  55. allowWhitespaceAfterColon = false)
  56. for kind, key, val in parseopt.getopt(x):
  57. echo kind, " ", key
  58. block:
  59. echo "parseoptNoVal"
  60. # test NoVal mode with custom cmdline arguments
  61. var argv = "--left --debug:3 -l -r:2 --debug 2 --debug=1 -r1 -r=0 -lr4 --debug:"
  62. var p = parseopt.initOptParser(argv,
  63. shortNoVal = {'l'}, longNoVal = @["left"])
  64. for kind, key, val in parseopt.getopt(p):
  65. echo "kind: ", kind, "\tkey:val -- ", key, ":", val
  66. import osproc, os, strutils
  67. from stdtest/specialpaths import buildDir
  68. import stdtest/unittest_light
  69. block: # fix #9951
  70. template runTest(parseoptCustom) =
  71. var p = parseoptCustom.initOptParser(@["echo \"quoted\""])
  72. let expected = when defined(windows):
  73. """"echo \"quoted\"""""
  74. else:
  75. """'echo "quoted"'"""
  76. assertEquals parseoptCustom.cmdLineRest(p), expected
  77. doAssert "a5'b" == "a5\'b"
  78. let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
  79. var p2 = parseoptCustom.initOptParser(args)
  80. let expected2 = when defined(windows):
  81. """a1b "a2 b" "" a4\"b a5'b a6\b a7'b"""
  82. else:
  83. """a1b 'a2 b' '' 'a4"b' 'a5'"'"'b' 'a6\b' 'a7'"'"'b'"""
  84. doAssert "a5'b" == "a5\'b"
  85. assertEquals parseoptCustom.cmdLineRest(p2), expected2
  86. runTest(parseopt)
  87. block: # fix #9842
  88. let exe = buildDir / "D20190112T145450".addFileExt(ExeExt)
  89. defer:
  90. when not defined(windows):
  91. # workaround #10359 ; innocuous to skip since we're saving under `buildDir`
  92. removeFile exe
  93. let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
  94. let cmd = "$# c -r --verbosity:0 -o:$# -d:testament_tparseopt $# $#" %
  95. [getCurrentCompilerExe(), exe, currentSourcePath(),
  96. args.quoteShellCommand]
  97. var ret = execCmdEx(cmd, options = {})
  98. if ret.exitCode != 0:
  99. # before bug fix, running cmd would show:
  100. # sh: -c: line 0: unexpected EOF while looking for matching `"'\n
  101. echo "exitCode: ", ret.exitCode, " cmd:", cmd
  102. doAssert false
  103. stripLineEnd(ret.output)
  104. assertEquals ret.output,
  105. """
  106. @["a1b", "a2 b", "", "a4\"b", "a5\'b", "a6\\b", "a7\'b"]
  107. arg 0 ai.len:3 :{a1b}
  108. arg 1 ai.len:4 :{a2 b}
  109. arg 2 ai.len:0 :{}
  110. arg 3 ai.len:4 :{a4"b}
  111. arg 4 ai.len:4 :{a5'b}
  112. arg 5 ai.len:4 :{a6\b}
  113. arg 6 ai.len:4 :{a7'b}"""