Make parseopt available on all backends (#17009)
* Make parseopt available on all backends * fix spaces * fix getopt and update prelude * no crazy unnecessary version checks, use doAssert
This commit is contained in:
parent
4326f743d0
commit
fad8439b8d
5 changed files with 165 additions and 144 deletions
|
|
@ -147,6 +147,14 @@ provided by the operating system.
|
||||||
issues like https://github.com/nim-lang/Nim/issues/13063 (which affected error messages)
|
issues like https://github.com/nim-lang/Nim/issues/13063 (which affected error messages)
|
||||||
for modules importing `std/wrapnils`.
|
for modules importing `std/wrapnils`.
|
||||||
|
|
||||||
|
- `parseopt.initOptParser` has been made available and `parseopt` has been
|
||||||
|
added back to `prelude` for all backends. Previously `initOptParser` was
|
||||||
|
unavailable if the `os` module did not have `paramCount` or `paramStr`,
|
||||||
|
but the use of these in `initOptParser` were conditionally to the runtime
|
||||||
|
arguments passed to it, so `initOptParser` has been changed to raise
|
||||||
|
`ValueError` when the real command line is not available. `parseopt` was
|
||||||
|
previously excluded from `prelude` for JS, as it could not be imported.
|
||||||
|
|
||||||
## Language changes
|
## Language changes
|
||||||
|
|
||||||
- `nimscript` now handles `except Exception as e`.
|
- `nimscript` now handles `except Exception as e`.
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,6 @@
|
||||||
## Same as:
|
## Same as:
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## import os, strutils, times, parseutils, hashes, tables, sets, sequtils
|
## import os, strutils, times, parseutils, hashes, tables, sets, sequtils, parseopt
|
||||||
## when not defined(js): import parseopt
|
|
||||||
|
|
||||||
import os, strutils, times, parseutils, hashes, tables, sets, sequtils
|
import os, strutils, times, parseutils, hashes, tables, sets, sequtils, parseopt
|
||||||
when not defined(js): import parseopt
|
|
||||||
|
|
|
||||||
|
|
@ -192,17 +192,14 @@ proc parseWord(s: string, i: int, w: var string,
|
||||||
add(w, s[result])
|
add(w, s[result])
|
||||||
inc(result)
|
inc(result)
|
||||||
|
|
||||||
when declared(os.paramCount):
|
|
||||||
# we cannot provide this for NimRtl creation on Posix, because we can't
|
|
||||||
# access the command line arguments then!
|
|
||||||
|
|
||||||
proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
|
proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
|
||||||
longNoVal: seq[string] = @[];
|
longNoVal: seq[string] = @[];
|
||||||
allowWhitespaceAfterColon = true): OptParser =
|
allowWhitespaceAfterColon = true): OptParser =
|
||||||
## Initializes the command line parser.
|
## Initializes the command line parser.
|
||||||
##
|
##
|
||||||
## If ``cmdline == ""``, the real command line as provided by the
|
## If ``cmdline == ""``, the real command line as provided by the
|
||||||
## ``os`` module is retrieved instead.
|
## ``os`` module is retrieved instead if it is available. If the
|
||||||
|
## command line is not available, a `ValueError` will be raised.
|
||||||
##
|
##
|
||||||
## ``shortNoVal`` and ``longNoVal`` are used to specify which options
|
## ``shortNoVal`` and ``longNoVal`` are used to specify which options
|
||||||
## do not take values. See the `documentation about these
|
## do not take values. See the `documentation about these
|
||||||
|
|
@ -226,9 +223,15 @@ when declared(os.paramCount):
|
||||||
if cmdline != "":
|
if cmdline != "":
|
||||||
result.cmds = parseCmdLine(cmdline)
|
result.cmds = parseCmdLine(cmdline)
|
||||||
else:
|
else:
|
||||||
result.cmds = newSeq[string](os.paramCount())
|
when declared(paramCount):
|
||||||
for i in countup(1, os.paramCount()):
|
result.cmds = newSeq[string](paramCount())
|
||||||
result.cmds[i-1] = os.paramStr(i)
|
for i in countup(1, paramCount()):
|
||||||
|
result.cmds[i-1] = paramStr(i)
|
||||||
|
else:
|
||||||
|
# we cannot provide this for NimRtl creation on Posix, because we can't
|
||||||
|
# access the command line arguments then!
|
||||||
|
doAssert false, "empty command line given but" &
|
||||||
|
" real command line is not accessible"
|
||||||
|
|
||||||
result.kind = cmdEnd
|
result.kind = cmdEnd
|
||||||
result.key = ""
|
result.key = ""
|
||||||
|
|
@ -240,8 +243,10 @@ when declared(os.paramCount):
|
||||||
## Initializes the command line parser.
|
## Initializes the command line parser.
|
||||||
##
|
##
|
||||||
## If ``cmdline.len == 0``, the real command line as provided by the
|
## If ``cmdline.len == 0``, the real command line as provided by the
|
||||||
## ``os`` module is retrieved instead. Behavior of the other parameters
|
## ``os`` module is retrieved instead if it is available. If the
|
||||||
## remains the same as in `initOptParser(string, ...)
|
## command line is not available, a `ValueError` will be raised.
|
||||||
|
## Behavior of the other parameters remains the same as in
|
||||||
|
## `initOptParser(string, ...)
|
||||||
## <#initOptParser,string,set[char],seq[string]>`_.
|
## <#initOptParser,string,set[char],seq[string]>`_.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
|
|
@ -263,9 +268,15 @@ when declared(os.paramCount):
|
||||||
for i in 0..<cmdline.len:
|
for i in 0..<cmdline.len:
|
||||||
result.cmds[i] = cmdline[i]
|
result.cmds[i] = cmdline[i]
|
||||||
else:
|
else:
|
||||||
result.cmds = newSeq[string](os.paramCount())
|
when declared(paramCount):
|
||||||
for i in countup(1, os.paramCount()):
|
result.cmds = newSeq[string](paramCount())
|
||||||
result.cmds[i-1] = os.paramStr(i)
|
for i in countup(1, paramCount()):
|
||||||
|
result.cmds[i-1] = paramStr(i)
|
||||||
|
else:
|
||||||
|
# we cannot provide this for NimRtl creation on Posix, because we can't
|
||||||
|
# access the command line arguments then!
|
||||||
|
doAssert false, "empty command line given but" &
|
||||||
|
" real command line is not accessible"
|
||||||
result.kind = cmdEnd
|
result.kind = cmdEnd
|
||||||
result.key = ""
|
result.key = ""
|
||||||
result.val = ""
|
result.val = ""
|
||||||
|
|
@ -366,6 +377,7 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
|
||||||
inc p.idx
|
inc p.idx
|
||||||
p.pos = 0
|
p.pos = 0
|
||||||
|
|
||||||
|
when declared(quoteShellCommand):
|
||||||
proc cmdLineRest*(p: OptParser): string {.rtl, extern: "npo$1".} =
|
proc cmdLineRest*(p: OptParser): string {.rtl, extern: "npo$1".} =
|
||||||
## Retrieves the rest of the command line that has not been parsed yet.
|
## Retrieves the rest of the command line that has not been parsed yet.
|
||||||
##
|
##
|
||||||
|
|
@ -442,8 +454,7 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
|
||||||
if p.kind == cmdEnd: break
|
if p.kind == cmdEnd: break
|
||||||
yield (p.kind, p.key, p.val)
|
yield (p.kind, p.key, p.val)
|
||||||
|
|
||||||
when declared(initOptParser):
|
iterator getopt*(cmdline: seq[string] = @[],
|
||||||
iterator getopt*(cmdline: seq[string] = commandLineParams(),
|
|
||||||
shortNoVal: set[char] = {}, longNoVal: seq[string] = @[]):
|
shortNoVal: set[char] = {}, longNoVal: seq[string] = @[]):
|
||||||
tuple[kind: CmdLineKind, key, val: string] =
|
tuple[kind: CmdLineKind, key, val: string] =
|
||||||
## Convenience iterator for iterating over command line arguments.
|
## Convenience iterator for iterating over command line arguments.
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ import std/[
|
||||||
|
|
||||||
# Parsers:
|
# Parsers:
|
||||||
htmlparser, json, lexbase, parsecfg, parsecsv, parsesql, parsexml,
|
htmlparser, json, lexbase, parsecfg, parsecsv, parsesql, parsexml,
|
||||||
# fails: parseopt
|
parseopt,
|
||||||
|
|
||||||
# XML processing:
|
# XML processing:
|
||||||
xmltree, xmlparser,
|
xmltree, xmlparser,
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ import std/[
|
||||||
|
|
||||||
# Parsers:
|
# Parsers:
|
||||||
htmlparser, json, lexbase, parsecfg, parsecsv, parsesql, parsexml,
|
htmlparser, json, lexbase, parsecfg, parsecsv, parsesql, parsexml,
|
||||||
# fails: parseopt
|
parseopt,
|
||||||
|
|
||||||
# XML processing:
|
# XML processing:
|
||||||
xmltree, xmlparser,
|
xmltree, xmlparser,
|
||||||
|
|
@ -114,3 +114,7 @@ block: # cpDir, cpFile, dirExists, fileExists, mkDir, mvDir, mvFile, rmDir, rmF
|
||||||
doAssert dirExists(subDir2)
|
doAssert dirExists(subDir2)
|
||||||
mvDir(subDir2, subDir)
|
mvDir(subDir2, subDir)
|
||||||
rmDir(dname)
|
rmDir(dname)
|
||||||
|
|
||||||
|
block:
|
||||||
|
# check parseopt can get command line:
|
||||||
|
discard initOptParser()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue