.Q.def/.Q.opt to handle toggle option?

I’m curious whether the combo .Q.def and .Q.opt can be used to handle toggle options. For example, given the following command line:

q myscript.q -log info -debug

I want .Q.def-.Q.opt to return something like:

``logdebug!(info;1b)`

I tried the following, but it didn’t work (it seems that .Q.def always assumes a “-opt value” kind of format):

.Q.def[logdebug!(;0b)] .Q.opt .z.x`

The result became:

``logdebug!(info;0b)`

Which is indistinguishable from

q myscript.q -log info

So, is there any elegant way to handle toggle type of command options in q?

Hi, 

Try this

q myscript.q -log info -debug 1

And also you can use .z.x to read command line in the raw format … 

Thanks, Igor. Yes, I’m aware of this alternative. But it just looks ugly (and makes use wonder what does 2 or 3 mean for this option)… thus my original question of whether there is an elegant way to handle such toggle options.

Hi Flying,

you could try something like this:

~ $ q myscript.q -log info -debug

q).Q.def[enlist[log]!enlist].Q.opt .z.x
log  | info
debug|

q)o:.Q.def[enlist[`log]!enlist`].Q.opt .z.x
q)`debug in key o
1b

Regards,

Shari

Hi Flying, 
here is my previous answer … is it ok ?

 

q test.q  -log info -debug 1

 q).Q.def[`log`debug!(`;0b)] .Q.opt .z.x

log  | `info

debug| 1b

q test.q  -log info -debug 0 

q).Q.def[`log`debug!(`;0b)] .Q.opt .z.x

log  | `info

debug| 0b

??, 1 ???. 2018 ?. ? 12:14, Flying <flying.oe@gmail.com>:

Thanks, Igor. Yes, I’m aware of this alternative. But it just looks ugly (and makes use wonder what does 2 or 3 mean for this option)… thus my original question of whether there is an elegant way to handle such toggle options.

I think you can achieve what you want by defaulting the definition to 1b and then using a “not” switch

>q -log info -debug

q)@[;debug;not].Q.def[logdebug!(;1b)].Q.opt .z.x

log  | `info

debug| 1b

 

>q -log info

q)@[;debug;not].Q.def[logdebug!(;1b)].Q.opt .z.x

log  | `info

debug| 0b

Terry

Thanks, Terry. This seems a workable alternative. Though I still have one little ‘complain’ about this approach – it’s unintuitive to code maintainers / users sometimes.

Case 1: a maintainer checks for the default value of an option by searching for .Q.def.  All he/she might notice is that `debug defaults to 1b. So he/she might be confused.

Case 2: a user, like Igor noted previously, might actually put in a command line like q -debug 1. Out of blue he/she has disabled debugging, which is exactly the opposite of what he/she wanted.

The alternative with `debug in key opts works better by avoiding such  confusions, IMHO, though the maintainer in Case 1 won’t notice that debug flag…
 
On Friday, November 2, 2018 at 3:14:09 AM UTC+8, TerryLynch wrote:

I think you can achieve what you want by defaulting the definition to 1b and then using a “not” switch

>q -log info -debug

q)@[;debug;not].Q.def[log<wbr>debug!(;1b)].Q.opt .z.x

log  | `info

debug| 1b

 

>q -log info

q)@[;debug;not].Q.def[log<wbr>debug!(;1b)].Q.opt .z.x

log  | `info

debug| 0b

Terry