verbose = 4
port = undefined
subcommand = dep
list = arg1,arg2
string = s_var
subnargs = cc,dd
extension for help and long opt
var extargsparse = require('extargsparse');
var util = require('util');
var commandline = `
{
"verbose|v" : "+",
"pair|p!optparse=pair_parse;opthelp=pair_help!" : [],
"even|e!jsonfunc=single_2_jsonfunc!" : [],
"clr_CA_name" : null,
"quot; : "*"
}
`;
var pair_parse = function (args, validx, keycls, params) {
'use strict';
var val;
if ((validx + 1) >= params.length) {
throw new Error(util.format('need 2 args for [++pair|+p]'));
}
val = args[keycls.optdest];
if (val === undefined || val === null) {
val = [];
}
val.push(params[validx]);
val.push(params[(validx + 1)]);
args[keycls.optdest] = val;
return 2;
};
exports.pair_parse = pair_parse;
var pair_help = function (keycls) {
'use strict';
keycls = keycls;
return '[first] [second]';
};
exports.pair_help = pair_help;
var single_2_jsonfunc = function (args, keycls, value) {
'use strict';
var setvalue;
var idx;
if (!Array.isArray(value)) {
throw new Error(util.format('not list value'));
}
setvalue = [];
idx = 0;
while (idx < value.length) {
setvalue.push(value[idx]);
idx += 2;
}
args[keycls.optdest] = setvalue;
return;
};
exports.single_2_jsonfunc = single_2_jsonfunc;
var options;
var parser;
var args;
options = extargsparse.ExtArgsOption();
options.longprefix = '++';
options.shortprefix = '+';
options.jsonlong = 'jsonfile';
options.helplong = 'usage';
options.helpshort = '?';
options.flagnochange = true;
parser = extargsparse.ExtArgsParse(options);
parser.load_command_line_string(commandline);
args = parser.parse_command_line();
console.log('verbose [%d]', args.verbose);
console.log('pair (%s)', args.pair);
console.log('args (%s)', args.args);
console.log('clr_CA_name (%s)', args.clr_CA_name);
console.log('event (%s)', args.even);
run help
node opthandle.js +?
output
extopthelp.js [OPTIONS] [args...]
[OPTIONS]
++jsonfile jsonfile json input file to get the value set
++usage|+? to display this help information
++verbose|+v verbose verbose inc
++pair|+p pair [first] [second]
++even|+e even even set default()
++clr_CA_name clr_CA_name clr_CA_name set default(null)
cc.json file
{
"even": ["good", "bad"]
}
node opthandle.js ++jsonfile cc.json ++pair cc ss rr +vvvv
opthelp
** help format information string format like pair_help(keycls) keycls is the parse object to handle ,it can be
optparse
** parse function for opt
like
def parse_opt(args,validx,keycls,params):
*** args is the return value from the parse_command_line
validx is the value index in the params
keycls is the option object you can use optdest for the destination
params is the command line all in
jsonfunc
** json value set function for opt
like
def json_opt(args,keycls,value):
** args is the return value from the parse_command_line
keycls is the options object you can use optdest for destination
value is the value of json
extension get example
var extargsparse = require('extargsparse');
var util = require('util');
var load_s_1 = function (parser) {
'use strict';
var load1 = `
{
"verbose|v" : "+",
"port|p" : 3000,
"dep" : {
"list|l" : [],
"string|s" : "s_var",
"quot; : "+"
}
}
`;
parser.load_command_line_string(load1);
return parser;
};
var load_s_2 = function (parser) {
'use strict';
var load2 = `
{
"rdep" : {
"list|L" : [],
"string|S" : "s_rdep",
"quot; : 2
}
}
`;
parser.load_command_line_string(load2);
return parser;
};
var debug_cmd_opts = function (parser, name) {
'use strict';
var opts;
var opt;
var idx;
var subcmds;
if (name === undefined || name === null) {
name = '';
}
opts = parser.get_cmdopts(name);
if (opts !== null) {
for (idx = 0; idx < opts.length; idx += 1) {
opt = opts[idx];
if (opt.typename !== 'args') {
console.log('[%s] opt %s', name, opt.longopt);
}
}
}
subcmds = parser.get_subcommands(name);
if (subcmds !== null) {
console.log('[%s] subcmds %s', name, subcmds);
}
return subcmds;
};
var debug_total;
var debug_total_c = function (parser, name) {
'use strict';
var subcmds;
if (name === undefined || name === null) {
name = '';
}
subcmds = debug_cmd_opts(parser, name);
if (subcmds !== null && subcmds.length > 0) {
var idx;
var c;
var cname;
for (idx = 0; idx < subcmds.length; idx += 1) {
c = subcmds[idx];
cname = '';
cname += util.format('%s', name);
if (cname.length > 0) {
cname += '.';
}
cname += c;
debug_total(parser, cname);
}
}
return;
};
debug_total = debug_total_c;
var parser;
parser = extargsparse.ExtArgsParse();
parser = load_s_1(parser);
parser = load_s_2(parser);
debug_total(parser);
nojson.js rdep [OPTIONS] args...
[OPTIONS]
--help|-h to display this help information
--list|-l list list set default()
--string|-s string string set default(s_rdep)
command
node nojson.js rdep --list cc --list bb 222
output
verbose [0]
subcommand [rdep]
list [cc,bb]
string [s_dep]
subnargs [222]
all key is with value of dict will be flag
** like this 'flag|f' : true
--flag or -f will set the False value for this ,default value is True
** like 'list|l' : []
--list or -l will append to the flag value ,default is []
if value is dict, the key is not start with special char ,it will be the sub command name
** for example 'get' : {
'connect|c' : 'http://www.google.com',
'newone|N' : false
} this will give the sub command with two flag (--get-connect or -c ) and ( --get-newone or -N ) default value is 'http://www.google.com' and False