Original: 9b8923844f
This commit is contained in:
Ryan Dahl 2018-12-24 10:28:01 -05:00 committed by GitHub
parent a5ad386887
commit b5f6f97234
21 changed files with 562 additions and 563 deletions

View file

@ -5,13 +5,14 @@
This repository contains collections of modules that create a standard library
for Deno.
| Collection | Description |
| ---------------------------- | --------------------------------------------------------------- |
| [colors](./colors/) | Modules that generate ANSI color codes for the console. |
| [net](./net/) | A framework for creating HTTP/HTTPS servers inspired by GoLang. |
| [path](./path/) | File path manipulation. |
| [flags](./flags/) | Command line arguments parser. |
| [logging](./logging/) | Command line logging |
| Collection | Description |
| --------------------- | --------------------------------------------------------------- |
| [colors](./colors/) | Modules that generate ANSI color codes for the console. |
| [net](./net/) | A framework for creating HTTP/HTTPS servers inspired by GoLang. |
| [path](./path/) | File path manipulation. |
| [flags](./flags/) | Command line arguments parser. |
| [logging](./logging/) | Command line logging |
---
Copyright 2018 the Deno authors. All rights reserved. MIT license.

View file

@ -6,8 +6,8 @@ inspired by packages like [chalk](https://www.npmjs.com/package/chalk) and
## Usage
The main modules exports a single function name `color` which is a function
that provides chaining to stack colors. Basic usage looks like this:
The main modules exports a single function name `color` which is a function that
provides chaining to stack colors. Basic usage looks like this:
```ts
import { color } from "https://deno.land/x/colors/main.ts";
@ -18,8 +18,8 @@ console.log(color.bgBlue.red.bold("Hello world!"));
## TODO
- Currently, it just assumes it is running in an environment that supports ANSI
escape code terminal coloring. It should actually detect, specifically
windows and adjust properly.
escape code terminal coloring. It should actually detect, specifically windows
and adjust properly.
- Test coverage is very basic at the moment.

View file

@ -3,6 +3,7 @@
These files are accessible for import via "https://deno.land/x/examples/".
Try it:
```
> deno https://deno.land/x/examples/gist.ts README.md
```
@ -10,6 +11,7 @@ Try it:
## Alias Based Installation
Add this to your `.bash_profile`
```
export GIST_TOKEN=ABC # Generate at https://github.com/settings/tokens
alias gist="deno https://deno.land/x/examples/gist.ts --allow-net --allow-env"

View file

@ -4,7 +4,7 @@ Command line arguments parser for Deno based on minimist
# Example
``` ts
```ts
import { args } from "deno";
import { parse } from "https://deno.land/x/flags/index.ts";
@ -32,8 +32,8 @@ $ deno example.ts -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
## const parsedArgs = parse(args, options = {});
`parsedArgs._` contains all the arguments that didn't have an option associated with
them.
`parsedArgs._` contains all the arguments that didn't have an option associated
with them.
Numeric-looking arguments will be returned as numbers unless `options.string` or
`options.boolean` is set for that argument name.
@ -42,18 +42,19 @@ Any arguments after `'--'` will not be parsed and will end up in `parsedArgs._`.
options can be:
* `options.string` - a string or array of strings argument names to always treat as
strings
* `options.boolean` - a boolean, string or array of strings to always treat as
booleans. if `true` will treat all double hyphenated arguments without equal signs
as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`)
* `options.alias` - an object mapping string names to strings or arrays of string
argument names to use as aliases
* `options.default` - an object mapping string argument names to default values
* `options.stopEarly` - when true, populate `parsedArgs._` with everything after the
first non-option
* `options['--']` - when true, populate `parsedArgs._` with everything before the `--`
and `parsedArgs['--']` with everything after the `--`. Here's an example:
* `options.unknown` - a function which is invoked with a command line parameter not
defined in the `options` configuration object. If the function returns `false`, the
unknown option is not added to `parsedArgs`.
- `options.string` - a string or array of strings argument names to always treat
as strings
- `options.boolean` - a boolean, string or array of strings to always treat as
booleans. if `true` will treat all double hyphenated arguments without equal
signs as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`)
- `options.alias` - an object mapping string names to strings or arrays of
string argument names to use as aliases
- `options.default` - an object mapping string argument names to default values
- `options.stopEarly` - when true, populate `parsedArgs._` with everything after
the first non-option
- `options['--']` - when true, populate `parsedArgs._` with everything before
the `--` and `parsedArgs['--']` with everything after the `--`. Here's an
example:
- `options.unknown` - a function which is invoked with a command line parameter
not defined in the `options` configuration object. If the function returns
`false`, the unknown option is not added to `parsedArgs`.

View file

@ -3,30 +3,30 @@ import { parse } from "../index.ts";
// flag boolean true (default all --args to boolean)
test(function flagBooleanTrue() {
const argv = parse(['moo', '--honk', 'cow'], {
boolean: true
});
assertEqual(argv, {
honk: true,
_: ['moo', 'cow']
});
assertEqual(typeof argv.honk, 'boolean');
const argv = parse(["moo", "--honk", "cow"], {
boolean: true
});
assertEqual(argv, {
honk: true,
_: ["moo", "cow"]
});
assertEqual(typeof argv.honk, "boolean");
});
// flag boolean true only affects double hyphen arguments without equals signs
test(function flagBooleanTrueOnlyAffectsDoubleDash() {
var argv = parse(['moo', '--honk', 'cow', '-p', '55', '--tacos=good'], {
boolean: true
});
assertEqual(argv, {
honk: true,
tacos: 'good',
p: 55,
_: ['moo', 'cow']
});
assertEqual(typeof argv.honk, 'boolean');
var argv = parse(["moo", "--honk", "cow", "-p", "55", "--tacos=good"], {
boolean: true
});
assertEqual(argv, {
honk: true,
tacos: "good",
p: 55,
_: ["moo", "cow"]
});
assertEqual(typeof argv.honk, "boolean");
});

View file

@ -2,157 +2,157 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function flagBooleanDefaultFalse() {
const argv = parse(['moo'], {
boolean: ['t', 'verbose'],
default: { verbose: false, t: false }
});
assertEqual(argv, {
verbose: false,
t: false,
_: ['moo']
});
assertEqual(typeof argv.verbose, 'boolean');
assertEqual(typeof argv.t, 'boolean');
const argv = parse(["moo"], {
boolean: ["t", "verbose"],
default: { verbose: false, t: false }
});
assertEqual(argv, {
verbose: false,
t: false,
_: ["moo"]
});
assertEqual(typeof argv.verbose, "boolean");
assertEqual(typeof argv.t, "boolean");
});
test(function booleanGroups() {
const argv = parse([ '-x', '-z', 'one', 'two', 'three' ], {
boolean: ['x','y','z']
});
assertEqual(argv, {
x : true,
y : false,
z : true,
_ : [ 'one', 'two', 'three' ]
});
assertEqual(typeof argv.x, 'boolean');
assertEqual(typeof argv.y, 'boolean');
assertEqual(typeof argv.z, 'boolean');
const argv = parse(["-x", "-z", "one", "two", "three"], {
boolean: ["x", "y", "z"]
});
assertEqual(argv, {
x: true,
y: false,
z: true,
_: ["one", "two", "three"]
});
assertEqual(typeof argv.x, "boolean");
assertEqual(typeof argv.y, "boolean");
assertEqual(typeof argv.z, "boolean");
});
test(function booleanAndAliasWithChainableApi() {
const aliased = [ '-h', 'derp' ];
const regular = [ '--herp', 'derp' ];
const opts = {
herp: { alias: 'h', boolean: true }
};
const aliasedArgv = parse(aliased, {
boolean: 'herp',
alias: { h: 'herp' }
});
const propertyArgv = parse(regular, {
boolean: 'herp',
alias: { h: 'herp' }
});
const expected = {
herp: true,
h: true,
'_': [ 'derp' ]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const opts = {
herp: { alias: "h", boolean: true }
};
const aliasedArgv = parse(aliased, {
boolean: "herp",
alias: { h: "herp" }
});
const propertyArgv = parse(regular, {
boolean: "herp",
alias: { h: "herp" }
});
const expected = {
herp: true,
h: true,
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
});
test(function booleanAndAliasWithOptionsHash() {
const aliased = [ '-h', 'derp' ];
const regular = [ '--herp', 'derp' ];
const opts = {
alias: { 'h': 'herp' },
boolean: 'herp'
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
'_': [ 'derp' ]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const opts = {
alias: { h: "herp" },
boolean: "herp"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
});
test(function booleanAndAliasArrayWithOptionsHash() {
const aliased = [ '-h', 'derp' ];
const regular = [ '--herp', 'derp' ];
const alt = [ '--harp', 'derp' ];
const opts = {
alias: { 'h': ['herp', 'harp'] },
boolean: 'h'
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const altPropertyArgv = parse(alt, opts);
const expected = {
harp: true,
herp: true,
h: true,
'_': [ 'derp' ]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
assertEqual(altPropertyArgv, expected);
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const alt = ["--harp", "derp"];
const opts = {
alias: { h: ["herp", "harp"] },
boolean: "h"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const altPropertyArgv = parse(alt, opts);
const expected = {
harp: true,
herp: true,
h: true,
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
assertEqual(altPropertyArgv, expected);
});
test(function booleanAndAliasUsingExplicitTrue() {
const aliased = [ '-h', 'true' ];
const regular = [ '--herp', 'true' ];
const opts = {
alias: { h: 'herp' },
boolean: 'h'
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
'_': [ ]
};
const aliased = ["-h", "true"];
const regular = ["--herp", "true"];
const opts = {
alias: { h: "herp" },
boolean: "h"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
_: []
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
});
// regression, see https://github.com/substack/node-optimist/issues/71
// boolean and --x=true
test(function booleanAndNonBoolean() {
const parsed = parse(['--boool', '--other=true'], {
boolean: 'boool'
});
const parsed = parse(["--boool", "--other=true"], {
boolean: "boool"
});
assertEqual(parsed.boool, true);
assertEqual(parsed.other, 'true');
assertEqual(parsed.boool, true);
assertEqual(parsed.other, "true");
const parsed2 = parse(['--boool', '--other=false'], {
boolean: 'boool'
});
assertEqual(parsed2.boool, true);
assertEqual(parsed2.other, 'false');
const parsed2 = parse(["--boool", "--other=false"], {
boolean: "boool"
});
assertEqual(parsed2.boool, true);
assertEqual(parsed2.other, "false");
});
test(function booleanParsingTrue() {
const parsed = parse(['--boool=true'], {
default: {
boool: false
},
boolean: ['boool']
});
const parsed = parse(["--boool=true"], {
default: {
boool: false
},
boolean: ["boool"]
});
assertEqual(parsed.boool, true);
assertEqual(parsed.boool, true);
});
test(function booleanParsingFalse() {
const parsed = parse(['--boool=false'], {
default: {
boool: true
},
boolean: ['boool']
});
const parsed = parse(["--boool=false"], {
default: {
boool: true
},
boolean: ["boool"]
});
assertEqual(parsed.boool, false);
assertEqual(parsed.boool, false);
});

View file

@ -2,27 +2,22 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function hyphen() {
assertEqual(parse([ '-n', '-' ]), { n: '-', _: [] });
assertEqual(parse([ '-' ]), { _: [ '-' ] });
assertEqual(parse([ '-f-' ]), { f: '-', _: [] });
assertEqual(
parse([ '-b', '-' ], { boolean: 'b' }),
{ b: true, _: [ '-' ] }
);
assertEqual(
parse([ '-s', '-' ], { string: 's' }),
{ s: '-', _: [] }
);
assertEqual(parse(["-n", "-"]), { n: "-", _: [] });
assertEqual(parse(["-"]), { _: ["-"] });
assertEqual(parse(["-f-"]), { f: "-", _: [] });
assertEqual(parse(["-b", "-"], { boolean: "b" }), { b: true, _: ["-"] });
assertEqual(parse(["-s", "-"], { string: "s" }), { s: "-", _: [] });
});
test(function doubleDash() {
assertEqual(parse([ '-a', '--', 'b' ]), { a: true, _: [ 'b' ] });
assertEqual(parse([ '--a', '--', 'b' ]), { a: true, _: [ 'b' ] });
assertEqual(parse([ '--a', '--', 'b' ]), { a: true, _: [ 'b' ] });
assertEqual(parse(["-a", "--", "b"]), { a: true, _: ["b"] });
assertEqual(parse(["--a", "--", "b"]), { a: true, _: ["b"] });
assertEqual(parse(["--a", "--", "b"]), { a: true, _: ["b"] });
});
test(function moveArgsAfterDoubleDashIntoOwnArray() {
assertEqual(
parse([ '--name', 'John', 'before', '--', 'after' ], { '--': true }),
{ name: 'John', _: [ 'before' ], '--': [ 'after' ] });
assertEqual(
parse(["--name", "John", "before", "--", "after"], { "--": true }),
{ name: "John", _: ["before"], "--": ["after"] }
);
});

View file

@ -2,31 +2,30 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function booleanDefaultTrue() {
const argv = parse([], {
boolean: 'sometrue',
default: { sometrue: true }
});
assertEqual(argv.sometrue, true);
const argv = parse([], {
boolean: "sometrue",
default: { sometrue: true }
});
assertEqual(argv.sometrue, true);
});
test(function booleanDefaultFalse() {
const argv = parse([], {
boolean: 'somefalse',
default: { somefalse: false }
});
assertEqual(argv.somefalse, false);
const argv = parse([], {
boolean: "somefalse",
default: { somefalse: false }
});
assertEqual(argv.somefalse, false);
});
test(function booleanDefaultNull() {
const argv = parse([], {
boolean: 'maybe',
default: { maybe: null }
});
assertEqual(argv.maybe, null);
const argv2 = parse(['--maybe'], {
boolean: 'maybe',
default: { maybe: null }
});
assertEqual(argv2.maybe, true);
})
const argv = parse([], {
boolean: "maybe",
default: { maybe: null }
});
assertEqual(argv.maybe, null);
const argv2 = parse(["--maybe"], {
boolean: "maybe",
default: { maybe: null }
});
assertEqual(argv2.maybe, true);
});

View file

@ -2,18 +2,21 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function dottedAlias() {
const argv = parse(['--a.b', '22'], {default: {'a.b': 11}, alias: {'a.b': 'aa.bb'}});
assertEqual(argv.a.b, 22);
assertEqual(argv.aa.bb, 22);
const argv = parse(["--a.b", "22"], {
default: { "a.b": 11 },
alias: { "a.b": "aa.bb" }
});
assertEqual(argv.a.b, 22);
assertEqual(argv.aa.bb, 22);
});
test(function dottedDefault() {
const argv = parse('', {default: {'a.b': 11}, alias: {'a.b': 'aa.bb'}});
assertEqual(argv.a.b, 11);
assertEqual(argv.aa.bb, 11);
const argv = parse("", { default: { "a.b": 11 }, alias: { "a.b": "aa.bb" } });
assertEqual(argv.a.b, 11);
assertEqual(argv.aa.bb, 11);
});
test(function dottedDefaultWithNoAlias() {
const argv = parse('', {default: {'a.b': 11}});
assertEqual(argv.a.b, 11);
const argv = parse("", { default: { "a.b": 11 } });
assertEqual(argv.a.b, 11);
});

View file

@ -2,11 +2,11 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function short() {
const argv = parse([ '-b=123' ]);
assertEqual(argv, { b: 123, _: [] });
const argv = parse(["-b=123"]);
assertEqual(argv, { b: 123, _: [] });
});
test(function multiShort() {
const argv = parse([ '-a=whatever', '-b=robots' ]);
assertEqual(argv, { a: 'whatever', b: 'robots', _: [] });
const argv = parse(["-a=whatever", "-b=robots"]);
assertEqual(argv, { a: "whatever", b: "robots", _: [] });
});

View file

@ -2,24 +2,17 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function longOpts() {
assertEqual(
parse([ '--bool' ]),
{ bool : true, _ : [] },
);
assertEqual(
parse([ '--pow', 'xixxle' ]),
{ pow : 'xixxle', _ : [] },
);
assertEqual(
parse([ '--pow=xixxle' ]),
{ pow : 'xixxle', _ : [] },
);
assertEqual(
parse([ '--host', 'localhost', '--port', '555' ]),
{ host : 'localhost', port : 555, _ : [] },
);
assertEqual(
parse([ '--host=localhost', '--port=555' ]),
{ host : 'localhost', port : 555, _ : [] },
);
assertEqual(parse(["--bool"]), { bool: true, _: [] });
assertEqual(parse(["--pow", "xixxle"]), { pow: "xixxle", _: [] });
assertEqual(parse(["--pow=xixxle"]), { pow: "xixxle", _: [] });
assertEqual(parse(["--host", "localhost", "--port", "555"]), {
host: "localhost",
port: 555,
_: []
});
assertEqual(parse(["--host=localhost", "--port=555"]), {
host: "localhost",
port: 555,
_: []
});
});

View file

@ -2,33 +2,38 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function nums() {
const argv = parse([
'-x', '1234',
'-y', '5.67',
'-z', '1e7',
'-w', '10f',
'--hex', '0xdeadbeef',
'789'
]);
assertEqual(argv, {
x : 1234,
y : 5.67,
z : 1e7,
w : '10f',
hex : 0xdeadbeef,
_ : [ 789 ]
});
assertEqual(typeof argv.x, 'number');
assertEqual(typeof argv.y, 'number');
assertEqual(typeof argv.z, 'number');
assertEqual(typeof argv.w, 'string');
assertEqual(typeof argv.hex, 'number');
assertEqual(typeof argv._[0], 'number');
const argv = parse([
"-x",
"1234",
"-y",
"5.67",
"-z",
"1e7",
"-w",
"10f",
"--hex",
"0xdeadbeef",
"789"
]);
assertEqual(argv, {
x: 1234,
y: 5.67,
z: 1e7,
w: "10f",
hex: 0xdeadbeef,
_: [789]
});
assertEqual(typeof argv.x, "number");
assertEqual(typeof argv.y, "number");
assertEqual(typeof argv.z, "number");
assertEqual(typeof argv.w, "string");
assertEqual(typeof argv.hex, "number");
assertEqual(typeof argv._[0], "number");
});
test(function alreadyNumber() {
const argv = parse([ '-x', 1234, 789 ]);
assertEqual(argv, { x : 1234, _ : [ 789 ] });
assertEqual(typeof argv.x, 'number');
assertEqual(typeof argv._[0], 'number');
const argv = parse(["-x", 1234, 789]);
assertEqual(argv, { x: 1234, _: [789] });
assertEqual(typeof argv.x, "number");
assertEqual(typeof argv._[0], "number");
});

View file

@ -1,182 +1,192 @@
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function _arseArgs() {
assertEqual(
parse([ '--no-moo' ]),
{ moo : false, _ : [] },
);
assertEqual(
parse([ '-v', 'a', '-v', 'b', '-v', 'c' ]),
{ v : ['a','b','c'], _ : [] },
);
assertEqual(parse(["--no-moo"]), { moo: false, _: [] });
assertEqual(parse(["-v", "a", "-v", "b", "-v", "c"]), {
v: ["a", "b", "c"],
_: []
});
});
test(function comprehensive() {
assertEqual(
parse([
'--name=meowmers', 'bare', '-cats', 'woo',
'-h', 'awesome', '--multi=quux',
'--key', 'value',
'-b', '--bool', '--no-meep', '--multi=baz',
'--', '--not-a-flag', 'eek'
]),
{
c : true,
a : true,
t : true,
s : 'woo',
h : 'awesome',
b : true,
bool : true,
key : 'value',
multi : [ 'quux', 'baz' ],
meep : false,
name : 'meowmers',
_ : [ 'bare', '--not-a-flag', 'eek' ]
}
);
assertEqual(
parse([
"--name=meowmers",
"bare",
"-cats",
"woo",
"-h",
"awesome",
"--multi=quux",
"--key",
"value",
"-b",
"--bool",
"--no-meep",
"--multi=baz",
"--",
"--not-a-flag",
"eek"
]),
{
c: true,
a: true,
t: true,
s: "woo",
h: "awesome",
b: true,
bool: true,
key: "value",
multi: ["quux", "baz"],
meep: false,
name: "meowmers",
_: ["bare", "--not-a-flag", "eek"]
}
);
});
test(function flagBoolean() {
const argv = parse([ '-t', 'moo' ], { boolean: 't' });
assertEqual(argv, { t : true, _ : [ 'moo' ] });
assertEqual(typeof argv.t, 'boolean');
const argv = parse(["-t", "moo"], { boolean: "t" });
assertEqual(argv, { t: true, _: ["moo"] });
assertEqual(typeof argv.t, "boolean");
});
test(function flagBooleanValue() {
const argv = parse(['--verbose', 'false', 'moo', '-t', 'true'], {
boolean: [ 't', 'verbose' ],
default: { verbose: true }
});
assertEqual(argv, {
verbose: false,
t: true,
_: ['moo']
});
assertEqual(typeof argv.verbose, 'boolean');
assertEqual(typeof argv.t, 'boolean');
const argv = parse(["--verbose", "false", "moo", "-t", "true"], {
boolean: ["t", "verbose"],
default: { verbose: true }
});
assertEqual(argv, {
verbose: false,
t: true,
_: ["moo"]
});
assertEqual(typeof argv.verbose, "boolean");
assertEqual(typeof argv.t, "boolean");
});
test(function newlinesInParams() {
const args = parse([ '-s', "X\nX" ])
assertEqual(args, { _ : [], s : "X\nX" });
// reproduce in bash:
// VALUE="new
// line"
// deno program.js --s="$VALUE"
const args2 = parse([ "--s=X\nX" ])
assertEqual(args2, { _ : [], s : "X\nX" });
const args = parse(["-s", "X\nX"]);
assertEqual(args, { _: [], s: "X\nX" });
// reproduce in bash:
// VALUE="new
// line"
// deno program.js --s="$VALUE"
const args2 = parse(["--s=X\nX"]);
assertEqual(args2, { _: [], s: "X\nX" });
});
test(function strings() {
const s = parse([ '-s', '0001234' ], { string: 's' }).s;
assertEqual(s, '0001234');
assertEqual(typeof s, 'string');
const x = parse([ '-x', '56' ], { string: 'x' }).x;
assertEqual(x, '56');
assertEqual(typeof x, 'string');
const s = parse(["-s", "0001234"], { string: "s" }).s;
assertEqual(s, "0001234");
assertEqual(typeof s, "string");
const x = parse(["-x", "56"], { string: "x" }).x;
assertEqual(x, "56");
assertEqual(typeof x, "string");
});
test(function stringArgs() {
const s = parse([ ' ', ' ' ], { string: '_' })._;
assertEqual(s.length, 2);
assertEqual(typeof s[0], 'string');
assertEqual(s[0], ' ');
assertEqual(typeof s[1], 'string');
assertEqual(s[1], ' ');
const s = parse([" ", " "], { string: "_" })._;
assertEqual(s.length, 2);
assertEqual(typeof s[0], "string");
assertEqual(s[0], " ");
assertEqual(typeof s[1], "string");
assertEqual(s[1], " ");
});
test(function emptyStrings() {
const s = parse([ '-s' ], { string: 's' }).s;
assertEqual(s, '');
assertEqual(typeof s, 'string');
const s = parse(["-s"], { string: "s" }).s;
assertEqual(s, "");
assertEqual(typeof s, "string");
const str = parse([ '--str' ], { string: 'str' }).str;
assertEqual(str, '');
assertEqual(typeof str, 'string');
const str = parse(["--str"], { string: "str" }).str;
assertEqual(str, "");
assertEqual(typeof str, "string");
const letters = parse([ '-art' ], {
string: [ 'a', 't' ]
});
const letters = parse(["-art"], {
string: ["a", "t"]
});
assertEqual(letters.a, '');
assertEqual(letters.r, true);
assertEqual(letters.t, '');
assertEqual(letters.a, "");
assertEqual(letters.r, true);
assertEqual(letters.t, "");
});
test(function stringAndAlias() {
const x = parse([ '--str', '000123' ], {
string: 's',
alias: { s: 'str' }
});
const x = parse(["--str", "000123"], {
string: "s",
alias: { s: "str" }
});
assertEqual(x.str, '000123');
assertEqual(typeof x.str, 'string');
assertEqual(x.s, '000123');
assertEqual(typeof x.s, 'string');
assertEqual(x.str, "000123");
assertEqual(typeof x.str, "string");
assertEqual(x.s, "000123");
assertEqual(typeof x.s, "string");
const y = parse([ '-s', '000123' ], {
string: 'str',
alias: { str: 's' }
});
const y = parse(["-s", "000123"], {
string: "str",
alias: { str: "s" }
});
assertEqual(y.str, '000123');
assertEqual(typeof y.str, 'string');
assertEqual(y.s, '000123');
assertEqual(typeof y.s, 'string');
assertEqual(y.str, "000123");
assertEqual(typeof y.str, "string");
assertEqual(y.s, "000123");
assertEqual(typeof y.s, "string");
});
test(function slashBreak() {
assertEqual(
parse([ '-I/foo/bar/baz' ]),
{ I : '/foo/bar/baz', _ : [] }
);
assertEqual(
parse([ '-xyz/foo/bar/baz' ]),
{ x : true, y : true, z : '/foo/bar/baz', _ : [] }
);
assertEqual(parse(["-I/foo/bar/baz"]), { I: "/foo/bar/baz", _: [] });
assertEqual(parse(["-xyz/foo/bar/baz"]), {
x: true,
y: true,
z: "/foo/bar/baz",
_: []
});
});
test(function alias() {
const argv = parse([ '-f', '11', '--zoom', '55' ], {
alias: { z: 'zoom' }
});
assertEqual(argv.zoom, 55);
assertEqual(argv.z, argv.zoom);
assertEqual(argv.f, 11);
const argv = parse(["-f", "11", "--zoom", "55"], {
alias: { z: "zoom" }
});
assertEqual(argv.zoom, 55);
assertEqual(argv.z, argv.zoom);
assertEqual(argv.f, 11);
});
test(function multiAlias() {
const argv = parse([ '-f', '11', '--zoom', '55' ], {
alias: { z: [ 'zm', 'zoom' ] }
});
assertEqual(argv.zoom, 55);
assertEqual(argv.z, argv.zoom);
assertEqual(argv.z, argv.zm);
assertEqual(argv.f, 11);
const argv = parse(["-f", "11", "--zoom", "55"], {
alias: { z: ["zm", "zoom"] }
});
assertEqual(argv.zoom, 55);
assertEqual(argv.z, argv.zoom);
assertEqual(argv.z, argv.zm);
assertEqual(argv.f, 11);
});
test(function nestedDottedObjects() {
const argv = parse([
'--foo.bar', '3', '--foo.baz', '4',
'--foo.quux.quibble', '5', '--foo.quux.o_O',
'--beep.boop'
]);
assertEqual(argv.foo, {
bar : 3,
baz : 4,
quux : {
quibble : 5,
o_O : true
}
});
assertEqual(argv.beep, { boop : true });
});
const argv = parse([
"--foo.bar",
"3",
"--foo.baz",
"4",
"--foo.quux.quibble",
"5",
"--foo.quux.o_O",
"--beep.boop"
]);
assertEqual(argv.foo, {
bar: 3,
baz: 4,
quux: {
quibble: 5,
o_O: true
}
});
assertEqual(argv.beep, { boop: true });
});

View file

@ -2,56 +2,43 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function numbericShortArgs() {
assertEqual(parse([ '-n123' ]), { n: 123, _: [] });
assertEqual(
parse([ '-123', '456' ]),
{ 1: true, 2: true, 3: 456, _: [] }
);
assertEqual(parse(["-n123"]), { n: 123, _: [] });
assertEqual(parse(["-123", "456"]), { 1: true, 2: true, 3: 456, _: [] });
});
test(function short() {
assertEqual(
parse([ '-b' ]),
{ b : true, _ : [] },
);
assertEqual(
parse([ 'foo', 'bar', 'baz' ]),
{ _ : [ 'foo', 'bar', 'baz' ] },
);
assertEqual(
parse([ '-cats' ]),
{ c : true, a : true, t : true, s : true, _ : [] },
);
assertEqual(
parse([ '-cats', 'meow' ]),
{ c : true, a : true, t : true, s : 'meow', _ : [] },
);
assertEqual(
parse([ '-h', 'localhost' ]),
{ h : 'localhost', _ : [] },
);
assertEqual(
parse([ '-h', 'localhost', '-p', '555' ]),
{ h : 'localhost', p : 555, _ : [] },
);
assertEqual(parse(["-b"]), { b: true, _: [] });
assertEqual(parse(["foo", "bar", "baz"]), { _: ["foo", "bar", "baz"] });
assertEqual(parse(["-cats"]), { c: true, a: true, t: true, s: true, _: [] });
assertEqual(parse(["-cats", "meow"]), {
c: true,
a: true,
t: true,
s: "meow",
_: []
});
assertEqual(parse(["-h", "localhost"]), { h: "localhost", _: [] });
assertEqual(parse(["-h", "localhost", "-p", "555"]), {
h: "localhost",
p: 555,
_: []
});
});
test(function mixedShortBoolAndCapture() {
assertEqual(
parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
{
f : true, p : 555, h : 'localhost',
_ : [ 'script.js' ]
}
);
assertEqual(parse(["-h", "localhost", "-fp", "555", "script.js"]), {
f: true,
p: 555,
h: "localhost",
_: ["script.js"]
});
});
test(function shortAndLong() {
assertEqual(
parse([ '-h', 'localhost', '-fp', '555', 'script.js' ]),
{
f : true, p : 555, h : 'localhost',
_ : [ 'script.js' ]
}
);
assertEqual(parse(["-h", "localhost", "-fp", "555", "script.js"]), {
f: true,
p: 555,
h: "localhost",
_: ["script.js"]
});
});

View file

@ -3,12 +3,12 @@ import { parse } from "../index.ts";
// stops parsing on the first non-option when stopEarly is set
test(function stopParsing() {
const argv = parse(['--aaa', 'bbb', 'ccc', '--ddd'], {
stopEarly: true
});
const argv = parse(["--aaa", "bbb", "ccc", "--ddd"], {
stopEarly: true
});
assertEqual(argv, {
aaa: 'bbb',
_: ['ccc', '--ddd']
});
assertEqual(argv, {
aaa: "bbb",
_: ["ccc", "--ddd"]
});
});

View file

@ -2,95 +2,95 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function booleanAndAliasIsNotUnknown() {
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = [ '-h', 'true', '--derp', 'true' ];
const regular = [ '--herp', 'true', '-d', 'true' ];
const opts = {
alias: { h: 'herp' },
boolean: 'h',
unknown: unknownFn
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = ["-h", "true", "--derp", "true"];
const regular = ["--herp", "true", "-d", "true"];
const opts = {
alias: { h: "herp" },
boolean: "h",
unknown: unknownFn
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
assertEqual(unknown, ['--derp', '-d']);
assertEqual(unknown, ["--derp", "-d"]);
});
test(function flagBooleanTrueAnyDoubleHyphenArgumentIsNotUnknown() {
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const argv = parse(['--honk', '--tacos=good', 'cow', '-p', '55'], {
boolean: true,
unknown: unknownFn
});
assertEqual(unknown, ['--tacos=good', 'cow', '-p']);
assertEqual(argv, {
honk: true,
_: []
});
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const argv = parse(["--honk", "--tacos=good", "cow", "-p", "55"], {
boolean: true,
unknown: unknownFn
});
assertEqual(unknown, ["--tacos=good", "cow", "-p"]);
assertEqual(argv, {
honk: true,
_: []
});
});
test(function stringAndAliasIsNotUnkown() {
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = [ '-h', 'hello', '--derp', 'goodbye' ];
const regular = [ '--herp', 'hello', '-d', 'moon' ];
const opts = {
alias: { h: 'herp' },
string: 'h',
unknown: unknownFn
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = ["-h", "hello", "--derp", "goodbye"];
const regular = ["--herp", "hello", "-d", "moon"];
const opts = {
alias: { h: "herp" },
string: "h",
unknown: unknownFn
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
assertEqual(unknown, ['--derp', '-d']);
assertEqual(unknown, ["--derp", "-d"]);
});
test(function defaultAndAliasIsNotUnknown() {
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = [ '-h', 'hello' ];
const regular = [ '--herp', 'hello' ];
const opts = {
default: { 'h': 'bar' },
alias: { 'h': 'herp' },
unknown: unknownFn
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = ["-h", "hello"];
const regular = ["--herp", "hello"];
const opts = {
default: { h: "bar" },
alias: { h: "herp" },
unknown: unknownFn
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
assertEqual(unknown, []);
assertEqual(unknown, []);
});
test(function valueFollowingDoubleHyphenIsNotUnknown() {
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = [ '--bad', '--', 'good', 'arg' ];
const opts = {
'--': true,
unknown: unknownFn
};
const argv = parse(aliased, opts);
const unknown = [];
function unknownFn(arg) {
unknown.push(arg);
return false;
}
const aliased = ["--bad", "--", "good", "arg"];
const opts = {
"--": true,
unknown: unknownFn
};
const argv = parse(aliased, opts);
assertEqual(unknown, ['--bad']);
assertEqual(argv, {
'--': ['good', 'arg'],
'_': []
})
assertEqual(unknown, ["--bad"]);
assertEqual(argv, {
"--": ["good", "arg"],
_: []
});
});

View file

@ -2,5 +2,5 @@ import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import { parse } from "../index.ts";
test(function whitespaceShouldBeWhitespace() {
assertEqual(parse([ '-x', '\t' ]).x, '\t');
assertEqual(parse(["-x", "\t"]).x, "\t");
});

View file

@ -27,7 +27,7 @@ async function main() {
await checkVersion();
const prettier = run({
args: ["bash", "-c", "prettier --write *.ts **/*.ts"]
args: ["bash", "-c", "prettier --write *.ts */**/*.ts *.md */**/*.md"]
});
const s = await prettier.status();
exit(s.code);

View file

@ -2,11 +2,14 @@
Very much work in progress. Contributions welcome.
This library is heavily inspired by Python's [logging](https://docs.python.org/3/library/logging.html#logging.Logger.log) module, altough
it's not planned to be a direct port. Having separate loggers, handlers, formatters and filters gives developer very granular control over logging
which is most desirable for server side software.
This library is heavily inspired by Python's
[logging](https://docs.python.org/3/library/logging.html#logging.Logger.log)
module, altough it's not planned to be a direct port. Having separate loggers,
handlers, formatters and filters gives developer very granular control over
logging which is most desirable for server side software.
Todo:
- [ ] implement formatters
- [ ] implement `FileHandler`
- [ ] tests
- [ ] tests

View file

@ -1,26 +1,26 @@
import { BaseHandler } from '../handler.ts';
import { LogLevel } from '../levels.ts';
import { BaseHandler } from "../handler.ts";
import { LogLevel } from "../levels.ts";
export class ConsoleHandler extends BaseHandler {
_log(level, ...args) {
switch (level) {
case LogLevel.DEBUG:
console.log(...args);
return;
case LogLevel.INFO:
console.info(...args);
return;
case LogLevel.WARNING:
console.warn(...args);
return;
case LogLevel.ERROR:
console.error(...args);
return;
case LogLevel.CRITICAL:
console.error(...args);
return;
default:
return;
}
_log(level, ...args) {
switch (level) {
case LogLevel.DEBUG:
console.log(...args);
return;
case LogLevel.INFO:
console.info(...args);
return;
case LogLevel.WARNING:
console.warn(...args);
return;
case LogLevel.ERROR:
console.error(...args);
return;
case LogLevel.CRITICAL:
console.error(...args);
return;
default:
return;
}
}
}

View file

@ -3,5 +3,5 @@
Usage:
```ts
import * as path from 'https://deno.land/x/path/index.ts'
import * as path from "https://deno.land/x/path/index.ts";
```