sgmonda / stdio Goto Github PK
View Code? Open in Web Editor NEWThe de facto standard input/output manager for Node.js
The de facto standard input/output manager for Node.js
stdio.question("Question?", ['y', 'n'], function(error, answer){
if (answer == 'Y'){
console.log("Yes entered");
}else{
console.log("No entered");
}
});
Input doesn't handlet at all and callback never called.
If you have and set required:false
you still can't specify args:1
. Thus the optional parameters will always be an array when specified?
Is there a workaround?
In addition to taking an array as the third argument to stdio.question
for the valid responses (for backward compatibility), accept an object with the following keys/ For example:
{
echo: true, // default: true
regex: /[aAbBcC]+/, // default: /.*/
values: ['male', 'female'] // default: null/undefined/[]
}
Really, though, values
could be shorthand for regex
(/male|female/
in the example above).
I thought of this as a more general mechanism while entering #15, which this issue could supercede.
Test case 1:
{
config: { _meta_: { args: 1 } },
command: "node program.js foo",
expected: { args: ['foo'] },
},
Fails. Program output: Option "--_meta_" requires 1 arguments, but 0 were provided
Test case 2:
{
config: { move: { key: 'm', args: '*' } },
command: "node program.js -m 1 2 3",
expected: { move: ['1', '2', '3'] },
},
Fails. Actually returns: {move: true}
. I would've expected either {move: ['1', '2', '3']}
or {move: true, args: ['1', '2', '3']}
. It's an ambiguous command, because you're not currently allowing the user a way to signify the end of options (e.g., using --
). So as a workaround, we can add a dummy option like -e
to signify the end of options, but that's kinda kludgy. I tried to hack a --
implementation with the config option end: {key: '-' }
, but that didn't work. Key has to be a letter, heh. Oh well.
In some configurations, I can get around these ambiguity limitations by entering the "positional arguments" before the options, but that's really unintuitive. What we really need is the ability to do something like, node program.js -m 1 2 3 -- foo bar
. That would resolve the ambiguity issue -- it would make it clear that 1 2 3
are the values for the -m
switch, and that foo bar
are the positional arguments.
This is a rather hard problem to explain, so please bear with me. As you know, the loadtest library now uses stdio to parse arguments. It usually works very well, but I just got an interesting bug report: when run as a command, parsing seems to be broken for strings with "=" in them. Sample:
$ loadtest http://localhost:80/ -c 1 -n 1 -H "Cookie:SPRING_SECURITY_CONTEXT=ZmYzYjZmYjItZThjOS00ZmZhLTkyOWQtZDRjYzE3NmRmZWIy"
Options: {"args":["http://localhost:80/","ZmYzYjZmYjItZThjOS00ZmZhLTkyOWQtZDRjYzE3NmRmZWIy"],"concurrency":"1","maxRequests":"1","headers":"Cookie:SPRING_SECURITY_CONTEXT"}
The second line is a printout of options as parsed: it incorrectly parses two arguments http://localhost:80/
and ZmYzYjZmYjItZThjOS00ZmZhLTkyOWQtZDRjYzE3NmRmZWIy
, when the second one is inside one parameter -H
.
This does not happen when run from within node, as:
$ node bin/loadtest.js http://localhost:80/ -c 1 -n 1 -H "Cookie:SPRING_SECURITY_CONTEXT=ZmYzYjZmYjItZThjOS00ZmZhLTkyOWQtZDRjYzE3NmRmZWIy"
If you want you can test it with loadtest
, or just create a command in node.js using the bin
part in package.json
:
"bin": {
"loadtest": "bin/loadtest.js",
"testserver-loadtest": "bin/testserver.js"
},
the printing of help text for command-line options is great, except that if an option is defined to have a default value which evaluates as JS "falsey", the default value text is not shown. well, sometimes it's not entirely obvious that a program's boolean option defaults to false and it'd be nice for the help text to say so, like it does say if the default value happens to be true.
the same nonsense happens for default values of zero, too.
so stop evaluating and passing judgment on the values --- just print them. or if you're really dead-set on judging, at least do the appropriate JS thing and compare the value (using ===) to undefined, rather than dumping any "falsey" value.
Thanks for good project.
I thinks better always or optional there is --help -h
in output of help args.
If I define:
var options = stdio.getopt({
after: {key: 'a', args: 0, mandatory: false, description: 'After'},
before: {key: 'b', args: 0, mandatory: false, description: 'Before'},
});
Then I should be able to run:
command -ab
Specifying two short options at once, instead of:
command -a -b
This is a really useful module which I want to keep using.
I was using it a lot but haven't been doing much node in 2/3 yrs.
Version 0.2.7 ( 20K downloads in last wk ) still works, fairly intuitively, with no problems, as documented.
Install latest (2.1.1) and I'm looking at problems for over an hour now. Eg.
[bob@wsa test]$ ls
total 12K
941606057 0 drwxrwxr-x. 4 bob bob 103 Nov 23 11:44 .
186279429 0 drwxrwxr-x. 12 bob bob 192 Nov 23 11:42 ..
942952508 4.0K -rw-rw-r--. 1 bob bob 252 Nov 23 12:13 main.js
1007631616 0 drwxrwxr-x. 3 bob bob 66 Nov 23 11:43 nbproject
1007631638 0 drwxrwxr-x. 3 bob bob 45 Nov 23 11:50 node_modules
942958148 4.0K -rw-rw-r--. 1 bob bob 264 Nov 23 11:50 package.json
942958566 4.0K -rw-rw-r--. 1 bob bob 849 Nov 23 11:50 package-lock.json
[bob@wsa test]$ cat main.js
const stdio = require('stdio');
const cmdoptions = stdio.getopt(
{
username: {key: 'u', args:1, description: 'db username',required:false},
password: {key: 'p', args:1, description: 'db password',required:false}
}
);
[bob@wsa test]$ cat package.json
{
"name": "test",
"version": "1.0.0",
"keywords": [
"util",
"functional",
"server",
"client",
"browser"
],
"author": "bob",
"contributors": [],
"dependencies": {
"stdio": "^2.1.1"
}
}
[bob@wsa test]$ node main
Option "--username" requires 1 arguments, but 0 were provided
USAGE: node main [OPTION1] [OPTION2]... arg1 arg2...
The following options are supported:
-u, --username <ARG1> db username
-p, --password <ARG1> db password
Why the error message when neither is "required" ?
[bob@wsa test]$ npm uninstall stdio
removed 1 package, and audited 1 package in 172ms
found 0 vulnerabilities
[bob@wsa test]$ npm install [email protected]
added 1 package, and audited 2 packages in 1s
found 0 vulnerabilities
[bob@wsa test]$ cat package.json
{
"name": "test",
"version": "1.0.0",
"keywords": [
"util",
"functional",
"server",
"client",
"browser"
],
"author": "bob",
"contributors": [],
"dependencies": {
"stdio": "^0.2.7"
}
}
[bob@wsa test]$ node main
[bob@wsa test]$ node main --help
USAGE: node main [OPTION1] [OPTION2]... arg1 arg2...
The following options are supported:
-u, --username <ARG1> db username
-p, --password <ARG1> db password
[bob@wsa test]$
as expected.
try something else...
[bob@wsa test]$ cat main.js
import { getopt } from 'stdio';
const options = getopt({
username: {key: 'u', args:1, description: 'db username',required:false},
password: {key: 'p', args:1, description: 'db password',required:false}
});
[bob@wsa test]$ cat package.json
{
"name": "test",
"type": "module",
"version": "1.0.0",
"keywords": [
"util",
"functional",
"server",
"client",
"browser"
],
"author": "bob",
"contributors": [],
"dependencies": {
"stdio": "^2.1.1"
}
}
[bob@wsa test]$ node main
Option "--username" requires 1 arguments, but 0 were provided
USAGE: node main [OPTION1] [OPTION2]... arg1 arg2...
The following options are supported:
-u, --username <ARG1> db username
-p, --password <ARG1> db password
[bob@wsa test]$
Still error.
stdio.printHelp()
does not render default values, which are informative. Please include.
See this sample code:
const stdio = require('stdio');
var args = stdio.getopt({
foo: { args: '*' },
baz: { },
});
console.log(args);
It return correct:
# node test_stdio.js --foo bar --baz
{ foo: 'bar', baz: true }
But when I don't have baz
param:
const stdio = require('stdio');
var args = stdio.getopt({
foo: { args: '*' },
});
console.log(args);
The result is incorrect:
# node test_stdio.js --foo bar
{ foo: true }
See:
const stdio = require('stdio');
var args = stdio.getopt({
foo: { args: 1 , required: false},
});
console.log(args);
# node test_stdio.js
Option "--foo" requires 1 arguments, but 0 were provided
USAGE: node test_stdio.js [OPTION1] [OPTION2]... arg1 arg2...
The following options are supported:
--foo <ARG1>
The foo
param is not required. But I have error: requires 1 arguments, but 0 were provided
When trying to parse an empty argument for loadtest
I get this exception:
$ node bin/loadtest.js - -n 30 -keepalive --rps 10 http://milliearth.org/
/Users/afernandez/projects/loadtest/node_modules/stdio/main.js:163
if (expected.args === 1) {
^
TypeError: Cannot read property 'args' of null
at Object.module.exports.getopt (/Users/afernandez/projects/loadtest/node_modules/stdio/main.js:163:17)
at Object.<anonymous> (/Users/afernandez/projects/loadtest/bin/loadtest.js:18:21)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Note that there is a single dash -
at the beginning of the line. It should not crash but show usage.
Given (coffeescript):
args = require("stdio").getopt
meta:
key: 'm'
args: 1
description: "The metadata file"
mandatory: true
features:
key: 'f'
args: 1
description: "The features file"
mandatory: true
If I run with:
node merge.js -m loc.ark+=13960=t0000693r.meta.json -f loc.ark+=13960=t0000693r.feat.json
the parsed command line looks like:
{ createHelp: [Function],
printHelp: [Function],
meta: 'loc.ark+=13960',
args: [ 't0000693r.meta.json', 't0000693r.feat.json' ],
features: 'loc.ark+=13960' }
But the file names are correct. One file is named loc.ark+=13960=t0000693r.meta.json
and the other loc.ark+=13960=t0000693r.feat.json
. (I can't do anything about the names since they come out of another tool which parses what's called a "pairtree" directory structure).
The "=" sign in the filename is causing stdio to believe there's an assignment there when in fact it's not. I tried escaping the = with = but that didn't do anything. I suggest adding another option to the configuration (in addition to "key", "args", "description"...etc.) like "decodeAssignment": true | false (you can think of a better name for it)
-Boris
I think it would be nice if we could set a default value for each argument.
Also a range of allowable values or a set of allowable words can be specified for the value of the parameters.
Currently its hard to specify a type for a config, unless its an explicite param for getopts()
.
All used types should be exported along with the main entry point of the module. Thanks.
When pass the parameter that have multiple variable args as last parameter it just return true
as response for this parameter
Follow the bellow example:
# myfile.js
const args = getopt({
foo: { args: 1, required: true },
bar: { args: '*', required: true }
})
console.log(args)
When you try to use it with "bar" as last parameter:
node myfile.js --foo foo --bar bar some-bar
You'll get it
$ node myfile.js --foo foo --bar bar some-bar
{ foo: 'foo', bar: true }
But if you try to use "bar" as non-last parameter it'll works:
node myfile.js --bar bar some-bar --foo foo
# output: { bar: [ 'bar', 'some-bar' ], foo: 'foo' }
node app.js --value '-1pXM_DZMg'
Unknown option: "-p".
Yesterday I updated to stdio 0.2.x, and when trying to use printHelp()
it crashes:
/home/user/module/lib/code.js:376
return options.printHelp();
^
TypeError: Object #<Object> has no method 'printHelp'
at self.run (/home/user/module/lib/code.js:376:19)
What am I doing wrong?
Release 0.2.1 is a bit unhappy, since it's trying to use util.js, which is missing from the repository.
It's common for long options to contain -
. I can't figure out a way to add a name that contains a dash.
I was hoping either _
gets converted to -
, or that it's possible to specify a destination variable. I believe python's argparse works this way.
--lng '-121.98396'
Unknown option: -1
stdio.question puts the answer in lower case only. Why not allowing uppercase ?
I want to prompt the user for a password, but can't figure out how to tell stdio.question
how to ask for a password and ensure that no characters are being echoed to the screen.
I expected to be able to do something like this:
var pwd;
stdio.question('Password: ', {
echo: false
}, function(err,p) {
if (err) //...
pwd = p;
});
but I don't see it documented anywhere.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.