Skip to content

Commit 32f768f

Browse files
committed
Add new enhanced command-line argument parser
1 parent dbf07b3 commit 32f768f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/cargs.q

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Enhanced Command Line Argument Parser
2+
// Copyright (c) 2019 Sport Trades Ltd
3+
4+
// Documentation: https://github.com/BuaBook/kdb-common/wiki/cargs.q
5+
6+
.require.lib`type;
7+
8+
9+
/ This function provides additional parsing on top of the standard '.Q.opt' parsing for user-specified command line arguments
10+
/ @returns (Dict) Argument parameter as keys with the argument values
11+
/ @see .cargs.i.parse
12+
/ @see .z.x
13+
.cargs.get:{
14+
:.cargs.i.parse .z.x;
15+
};
16+
17+
/ This function provides addition parsing on top of the standard '.Q.opt' parsing for all command line arguments (including kdb
18+
/ internal single-character arguments
19+
/ @returns (Dict) Argument parameter as keys with the argument values
20+
/ @see .cargs.i.parse
21+
/ @see .z.X
22+
.cargs.getWithInternal:{
23+
:.cargs.i.parse .z.X;
24+
};
25+
26+
27+
/ Command line argument parser with additional functionality on top of the standard '.Q.opt' parsing:
28+
/ - Removes additional '-' when argument keys are specified in the more standard '--parameter' form
29+
/ - All argument values are a string (10h) with multi-argument values joined with spaces between them
30+
/ @returns (Dict) Argument parameter as keys with the argument values
31+
/ @throws IllegalArgumentException If the input is not a mixed list of strings
32+
/ @see .Q.opt
33+
.cargs.i.parse:{[cmdArgsStr]
34+
if[not .type.isMixedList cmdArgsStr;
35+
'"IllegalArgumentException";
36+
];
37+
38+
cmdArgs:.Q.opt cmdArgsStr;
39+
cmdArgs:" " sv/: cmdArgs;
40+
41+
cmdArgsK:key cmdArgs;
42+
cmdArgsK:@[cmdArgsK; where "-" = first each string cmdArgsK; { `$1_ string x }];
43+
44+
:cmdArgsK!value cmdArgs;
45+
};

0 commit comments

Comments
 (0)