3 ms·
Yep you're right, it's definitely not doing as much as getopt, though it's the same general use case. It's output is a dictionary representing your command line
by srazzaque 6y ago
Yep you're right, it's definitely not doing as much as getopt, though it's the same general use case. It's output is a dictionary representing your command line arguments, which you'd then use in your program to act accordingly.
It's implemented in K and comes standard with the distrubution. If you've got a KDB+/Q distribution you can view the actual function definition by executing it without an argument:
q).Q.opt
k){[o]x::$[#i:&o like"-[^0-9]*";i[0]#o;o];((`$1_*:)'o)!1_'o:i_o}
Disclaimer: I cannot read K, and I'd consider myself a beginner in Q, so the above is completely alien to me. But what that does, in words: any "-single-dash-params" is treated as an option. It splits your 'argv' by those options. Everything between the options become the 'values' for the prior most recently preceding option. If you've got a no-arg option, it just becomes a member in the dictionary without a corresponding value:
$ q -opt1 foo bar -opt2 baz -opt3
q).Q.opt .z.x
opt1| ("foo";"bar")
opt2| ,"baz"
opt3| ()
EDIT: formatting.