import std/parseopt, std/strutils var sep: string p = initOptParser() while true: p.next() case p.kind of cmdEnd, cmdArgument: break of cmdShortOption, cmdLongOption: continue let strseq = ["just","a","test"] # don't want just\ta\ttest output if p.val == r"\t": sep = "\t" else: sep = p.val echo(strseq.join(sep))
to achieve proper output?
$ cmdtest --sep:\t
just a test
Thank youOnly tested on Unix, but
proc foo(outsep=' ') = echo ord(outsep)
import cligen; dispatch foo
# literal 1-byte hard TAB arg => emits "9\n"
# literal 2-byte backslash-t also emits "9\n"
You may also like other things about cligen.I couldn't get it to work with std/parseopt, but c-blake's cligen seems to be the solution, and I'd recommend using it anyway:
import cligen
proc cmdtest(sep: string) =
const strseq = ["just","a","test"]
echo strseq.join(sep)
dispatch cmdtest
However, you still need to pass a literal tab to the program. In bash/zsh, it can be done like this:
cmdtest --sep:$'\t'
For a string arg, yes you do need that shell syntax to get a literal TAB.
Besides @xigoi's fine suggestions, you can also just set an environ variable called t='<Ctrl-V>TAB'. I do that for newlines sometimes (called n) where you don't even need the <Ctrl-V> keystroke (^V with typical default stty settings, anyway) - rather just quote-ENTER-quote. Once you have such a variable it's just "$t" or "$n". There is also "printf capture" as in tab="$(printf \\t)" (or tr capture or ..).
However, as I already showed (in my comment lines), cligen will unescape a single char type for you, though. . And if you hate that then all this is CLauthor-overridable replacing cligen/argcvt.nim definitions between import cligen and dispatch. Documentation could be better. Feel free to hack away at the cligen Wiki.
Thank you @xigoi, @cblake, unfortunately on Windows (my environment) the smart trick to pass the literal Tab to program
cmdtest --sep:$'\t'
on CMD shell produces
$'\t'a$'\t'test
and I didn't yet found alternatives. However on Windows PowerShell
.\cmdtest -s:`t
just a test
does work. However this OS environment + shell dependency does not make it the right way to go. While \t is pretty "standard" convention for Tab, `t is not. Thank you for your suggestion of cligen, it seems pretty powerful (the --help API auto-documentation alone is well worth to consider it).