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)
|
||||
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
|
||||
|
||||
- `nimscript` now handles `except Exception as e`.
|
||||
|
|
|
|||
|
|
@ -16,8 +16,6 @@
|
|||
## Same as:
|
||||
##
|
||||
## .. code-block:: nim
|
||||
## import os, strutils, times, parseutils, hashes, tables, sets, sequtils
|
||||
## when not defined(js): import parseopt
|
||||
## import os, strutils, times, parseutils, hashes, tables, sets, sequtils, parseopt
|
||||
|
||||
import os, strutils, times, parseutils, hashes, tables, sets, sequtils
|
||||
when not defined(js): import parseopt
|
||||
import os, strutils, times, parseutils, hashes, tables, sets, sequtils, parseopt
|
||||
|
|
|
|||
|
|
@ -192,17 +192,14 @@ proc parseWord(s: string, i: int, w: var string,
|
|||
add(w, s[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] = @[];
|
||||
allowWhitespaceAfterColon = true): OptParser =
|
||||
## Initializes the command line parser.
|
||||
##
|
||||
## 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
|
||||
## do not take values. See the `documentation about these
|
||||
|
|
@ -226,22 +223,30 @@ when declared(os.paramCount):
|
|||
if cmdline != "":
|
||||
result.cmds = parseCmdLine(cmdline)
|
||||
else:
|
||||
result.cmds = newSeq[string](os.paramCount())
|
||||
for i in countup(1, os.paramCount()):
|
||||
result.cmds[i-1] = os.paramStr(i)
|
||||
when declared(paramCount):
|
||||
result.cmds = newSeq[string](paramCount())
|
||||
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.key = ""
|
||||
result.val = ""
|
||||
|
||||
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
||||
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
||||
longNoVal: seq[string] = @[];
|
||||
allowWhitespaceAfterColon = true): OptParser =
|
||||
## Initializes the command line parser.
|
||||
##
|
||||
## If ``cmdline.len == 0``, the real command line as provided by the
|
||||
## ``os`` module is retrieved instead. Behavior of the other parameters
|
||||
## remains the same as in `initOptParser(string, ...)
|
||||
## ``os`` module is retrieved instead if it is available. If the
|
||||
## 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]>`_.
|
||||
##
|
||||
## See also:
|
||||
|
|
@ -263,9 +268,15 @@ when declared(os.paramCount):
|
|||
for i in 0..<cmdline.len:
|
||||
result.cmds[i] = cmdline[i]
|
||||
else:
|
||||
result.cmds = newSeq[string](os.paramCount())
|
||||
for i in countup(1, os.paramCount()):
|
||||
result.cmds[i-1] = os.paramStr(i)
|
||||
when declared(paramCount):
|
||||
result.cmds = newSeq[string](paramCount())
|
||||
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.key = ""
|
||||
result.val = ""
|
||||
|
|
@ -366,7 +377,8 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
|
|||
inc p.idx
|
||||
p.pos = 0
|
||||
|
||||
proc cmdLineRest*(p: OptParser): string {.rtl, extern: "npo$1".} =
|
||||
when declared(quoteShellCommand):
|
||||
proc cmdLineRest*(p: OptParser): string {.rtl, extern: "npo$1".} =
|
||||
## Retrieves the rest of the command line that has not been parsed yet.
|
||||
##
|
||||
## See also:
|
||||
|
|
@ -442,8 +454,7 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
|
|||
if p.kind == cmdEnd: break
|
||||
yield (p.kind, p.key, p.val)
|
||||
|
||||
when declared(initOptParser):
|
||||
iterator getopt*(cmdline: seq[string] = commandLineParams(),
|
||||
iterator getopt*(cmdline: seq[string] = @[],
|
||||
shortNoVal: set[char] = {}, longNoVal: seq[string] = @[]):
|
||||
tuple[kind: CmdLineKind, key, val: string] =
|
||||
## Convenience iterator for iterating over command line arguments.
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ import std/[
|
|||
|
||||
# Parsers:
|
||||
htmlparser, json, lexbase, parsecfg, parsecsv, parsesql, parsexml,
|
||||
# fails: parseopt
|
||||
parseopt,
|
||||
|
||||
# XML processing:
|
||||
xmltree, xmlparser,
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ import std/[
|
|||
|
||||
# Parsers:
|
||||
htmlparser, json, lexbase, parsecfg, parsecsv, parsesql, parsexml,
|
||||
# fails: parseopt
|
||||
parseopt,
|
||||
|
||||
# XML processing:
|
||||
xmltree, xmlparser,
|
||||
|
|
@ -114,3 +114,7 @@ block: # cpDir, cpFile, dirExists, fileExists, mkDir, mvDir, mvFile, rmDir, rmF
|
|||
doAssert dirExists(subDir2)
|
||||
mvDir(subDir2, subDir)
|
||||
rmDir(dname)
|
||||
|
||||
block:
|
||||
# check parseopt can get command line:
|
||||
discard initOptParser()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue