1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-30 16:40:57 -05:00
This commit is contained in:
Ryan Dahl 2018-12-24 10:28:01 -05:00 committed by GitHub
parent 731f03829c
commit 9b8923844f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 562 additions and 563 deletions

View file

@ -6,12 +6,13 @@ 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 |
---
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'], {
const argv = parse(["moo", "--honk", "cow"], {
boolean: true
});
assertEqual(argv, {
honk: true,
_: ['moo', 'cow']
_: ["moo", "cow"]
});
assertEqual(typeof argv.honk, 'boolean');
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'], {
var argv = parse(["moo", "--honk", "cow", "-p", "55", "--tacos=good"], {
boolean: true
});
assertEqual(argv, {
honk: true,
tacos: 'good',
tacos: "good",
p: 55,
_: ['moo', 'cow']
_: ["moo", "cow"]
});
assertEqual(typeof argv.honk, 'boolean');
assertEqual(typeof argv.honk, "boolean");
});

View file

@ -2,56 +2,56 @@ 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'],
const argv = parse(["moo"], {
boolean: ["t", "verbose"],
default: { verbose: false, t: false }
});
assertEqual(argv, {
verbose: false,
t: false,
_: ['moo']
_: ["moo"]
});
assertEqual(typeof argv.verbose, 'boolean');
assertEqual(typeof argv.t, 'boolean');
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']
const argv = parse(["-x", "-z", "one", "two", "three"], {
boolean: ["x", "y", "z"]
});
assertEqual(argv, {
x : true,
y : false,
z : true,
_ : [ 'one', 'two', 'three' ]
x: true,
y: false,
z: true,
_: ["one", "two", "three"]
});
assertEqual(typeof argv.x, 'boolean');
assertEqual(typeof argv.y, 'boolean');
assertEqual(typeof argv.z, 'boolean');
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 aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const opts = {
herp: { alias: 'h', boolean: true }
herp: { alias: "h", boolean: true }
};
const aliasedArgv = parse(aliased, {
boolean: 'herp',
alias: { h: 'herp' }
boolean: "herp",
alias: { h: "herp" }
});
const propertyArgv = parse(regular, {
boolean: 'herp',
alias: { h: 'herp' }
boolean: "herp",
alias: { h: "herp" }
});
const expected = {
herp: true,
h: true,
'_': [ 'derp' ]
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
@ -59,30 +59,30 @@ test(function booleanAndAliasWithChainableApi() {
});
test(function booleanAndAliasWithOptionsHash() {
const aliased = [ '-h', 'derp' ];
const regular = [ '--herp', 'derp' ];
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const opts = {
alias: { 'h': 'herp' },
boolean: 'herp'
alias: { h: "herp" },
boolean: "herp"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
'_': [ 'derp' ]
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
});
test(function booleanAndAliasArrayWithOptionsHash() {
const aliased = [ '-h', 'derp' ];
const regular = [ '--herp', 'derp' ];
const alt = [ '--harp', 'derp' ];
const aliased = ["-h", "derp"];
const regular = ["--herp", "derp"];
const alt = ["--harp", "derp"];
const opts = {
alias: { 'h': ['herp', 'harp'] },
boolean: 'h'
alias: { h: ["herp", "harp"] },
boolean: "h"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
@ -91,7 +91,7 @@ test(function booleanAndAliasArrayWithOptionsHash() {
harp: true,
herp: true,
h: true,
'_': [ 'derp' ]
_: ["derp"]
};
assertEqual(aliasedArgv, expected);
assertEqual(propertyArgv, expected);
@ -99,18 +99,18 @@ test(function booleanAndAliasArrayWithOptionsHash() {
});
test(function booleanAndAliasUsingExplicitTrue() {
const aliased = [ '-h', 'true' ];
const regular = [ '--herp', 'true' ];
const aliased = ["-h", "true"];
const regular = ["--herp", "true"];
const opts = {
alias: { h: 'herp' },
boolean: 'h'
alias: { h: "herp" },
boolean: "h"
};
const aliasedArgv = parse(aliased, opts);
const propertyArgv = parse(regular, opts);
const expected = {
herp: true,
h: true,
'_': [ ]
_: []
};
assertEqual(aliasedArgv, expected);
@ -120,38 +120,38 @@ test(function booleanAndAliasUsingExplicitTrue() {
// 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.other, "true");
const parsed2 = parse(['--boool', '--other=false'], {
boolean: 'boool'
const parsed2 = parse(["--boool", "--other=false"], {
boolean: "boool"
});
assertEqual(parsed2.boool, true);
assertEqual(parsed2.other, 'false');
assertEqual(parsed2.other, "false");
});
test(function booleanParsingTrue() {
const parsed = parse(['--boool=true'], {
const parsed = parse(["--boool=true"], {
default: {
boool: false
},
boolean: ['boool']
boolean: ["boool"]
});
assertEqual(parsed.boool, true);
});
test(function booleanParsingFalse() {
const parsed = parse(['--boool=false'], {
const parsed = parse(["--boool=false"], {
default: {
boool: true
},
boolean: ['boool']
boolean: ["boool"]
});
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' ] });
parse(["--name", "John", "before", "--", "after"], { "--": true }),
{ name: "John", _: ["before"], "--": ["after"] }
);
});

View file

@ -3,7 +3,7 @@ import { parse } from "../index.ts";
test(function booleanDefaultTrue() {
const argv = parse([], {
boolean: 'sometrue',
boolean: "sometrue",
default: { sometrue: true }
});
assertEqual(argv.sometrue, true);
@ -11,7 +11,7 @@ test(function booleanDefaultTrue() {
test(function booleanDefaultFalse() {
const argv = parse([], {
boolean: 'somefalse',
boolean: "somefalse",
default: { somefalse: false }
});
assertEqual(argv.somefalse, false);
@ -19,14 +19,13 @@ test(function booleanDefaultFalse() {
test(function booleanDefaultNull() {
const argv = parse([], {
boolean: 'maybe',
boolean: "maybe",
default: { maybe: null }
});
assertEqual(argv.maybe, null);
const argv2 = parse(['--maybe'], {
boolean: 'maybe',
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'}});
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'}});
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}});
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' ]);
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

@ -3,32 +3,37 @@ import { parse } from "../index.ts";
test(function nums() {
const argv = parse([
'-x', '1234',
'-y', '5.67',
'-z', '1e7',
'-w', '10f',
'--hex', '0xdeadbeef',
'789'
"-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 ]
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');
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,152 +1,157 @@
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'
"--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' ]
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' ],
const argv = parse(["--verbose", "false", "moo", "-t", "true"], {
boolean: ["t", "verbose"],
default: { verbose: true }
});
assertEqual(argv, {
verbose: false,
t: true,
_: ['moo']
_: ["moo"]
});
assertEqual(typeof argv.verbose, 'boolean');
assertEqual(typeof argv.t, 'boolean');
assertEqual(typeof argv.verbose, "boolean");
assertEqual(typeof argv.t, "boolean");
});
test(function newlinesInParams() {
const args = parse([ '-s', "X\nX" ])
assertEqual(args, { _ : [], 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" });
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 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 x = parse(["-x", "56"], { string: "x" }).x;
assertEqual(x, "56");
assertEqual(typeof x, "string");
});
test(function stringArgs() {
const s = parse([ ' ', ' ' ], { string: '_' })._;
const s = parse([" ", " "], { string: "_" })._;
assertEqual(s.length, 2);
assertEqual(typeof s[0], 'string');
assertEqual(s[0], ' ');
assertEqual(typeof s[1], 'string');
assertEqual(s[1], ' ');
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.a, "");
assertEqual(letters.r, true);
assertEqual(letters.t, '');
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' }
const argv = parse(["-f", "11", "--zoom", "55"], {
alias: { z: "zoom" }
});
assertEqual(argv.zoom, 55);
assertEqual(argv.z, argv.zoom);
@ -154,8 +159,8 @@ test(function alias() {
});
test(function multiAlias() {
const argv = parse([ '-f', '11', '--zoom', '55' ], {
alias: { z: [ 'zm', 'zoom' ] }
const argv = parse(["-f", "11", "--zoom", "55"], {
alias: { z: ["zm", "zoom"] }
});
assertEqual(argv.zoom, 55);
assertEqual(argv.z, argv.zoom);
@ -165,18 +170,23 @@ test(function multiAlias() {
test(function nestedDottedObjects() {
const argv = parse([
'--foo.bar', '3', '--foo.baz', '4',
'--foo.quux.quibble', '5', '--foo.quux.o_O',
'--beep.boop'
"--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
bar: 3,
baz: 4,
quux: {
quibble: 5,
o_O: true
}
});
assertEqual(argv.beep, { boop : 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'], {
const argv = parse(["--aaa", "bbb", "ccc", "--ddd"], {
stopEarly: true
});
assertEqual(argv, {
aaa: 'bbb',
_: ['ccc', '--ddd']
aaa: "bbb",
_: ["ccc", "--ddd"]
});
});

View file

@ -7,17 +7,17 @@ test(function booleanAndAliasIsNotUnknown() {
unknown.push(arg);
return false;
}
const aliased = [ '-h', 'true', '--derp', 'true' ];
const regular = [ '--herp', 'true', '-d', 'true' ];
const aliased = ["-h", "true", "--derp", "true"];
const regular = ["--herp", "true", "-d", "true"];
const opts = {
alias: { h: 'herp' },
boolean: 'h',
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() {
@ -26,11 +26,11 @@ test(function flagBooleanTrueAnyDoubleHyphenArgumentIsNotUnknown() {
unknown.push(arg);
return false;
}
const argv = parse(['--honk', '--tacos=good', 'cow', '-p', '55'], {
const argv = parse(["--honk", "--tacos=good", "cow", "-p", "55"], {
boolean: true,
unknown: unknownFn
});
assertEqual(unknown, ['--tacos=good', 'cow', '-p']);
assertEqual(unknown, ["--tacos=good", "cow", "-p"]);
assertEqual(argv, {
honk: true,
_: []
@ -43,17 +43,17 @@ test(function stringAndAliasIsNotUnkown() {
unknown.push(arg);
return false;
}
const aliased = [ '-h', 'hello', '--derp', 'goodbye' ];
const regular = [ '--herp', 'hello', '-d', 'moon' ];
const aliased = ["-h", "hello", "--derp", "goodbye"];
const regular = ["--herp", "hello", "-d", "moon"];
const opts = {
alias: { h: 'herp' },
string: 'h',
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() {
@ -62,11 +62,11 @@ test(function defaultAndAliasIsNotUnknown() {
unknown.push(arg);
return false;
}
const aliased = [ '-h', 'hello' ];
const regular = [ '--herp', 'hello' ];
const aliased = ["-h", "hello"];
const regular = ["--herp", "hello"];
const opts = {
default: { 'h': 'bar' },
alias: { 'h': 'herp' },
default: { h: "bar" },
alias: { h: "herp" },
unknown: unknownFn
};
const aliasedArgv = parse(aliased, opts);
@ -81,16 +81,16 @@ test(function valueFollowingDoubleHyphenIsNotUnknown() {
unknown.push(arg);
return false;
}
const aliased = [ '--bad', '--', 'good', 'arg' ];
const aliased = ["--bad", "--", "good", "arg"];
const opts = {
'--': true,
"--": true,
unknown: unknownFn
};
const argv = parse(aliased, opts);
assertEqual(unknown, ['--bad']);
assertEqual(unknown, ["--bad"]);
assertEqual(argv, {
'--': ['good', 'arg'],
'_': []
})
"--": ["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

View file

@ -1,5 +1,5 @@
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) {

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";
```