Skip to content

Commit

Permalink
fix($compile): Extended ts type for logger (#134)
Browse files Browse the repository at this point in the history
Logger interface handle many input types that were not supported. Also added a basic ts example.
  • Loading branch information
kinok authored and mattallty committed Feb 19, 2019
1 parent cfcd91d commit dd0a82e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ prog.parse(process.argv);
// ./myprog deploy myapp production --tail 100
```

Or else if you prefer `typescript`
```javascript
#!/usr/bin/env node
import * as prog from 'caporal';
prog
.version('1.0.0')
// you specify arguments using .argument()
// 'app' is required, 'env' is optional
.command('deploy', 'Deploy an application')
.argument('<app>', 'App to deploy', /^myapp|their-app$/)
.argument('[env]', 'Environment to deploy on', /^dev|staging|production$/, 'local')
// you specify options using .option()
// if --tail is passed, its value is required
.option('--tail <lines>', 'Tail <lines> lines of logs after deploy', prog.INT)
.action(function(args, options, logger) {
// args and options are objects
// args = {"app": "myapp", "env": "production"}
// options = {"tail" : 100}
});

prog.parse(process.argv);

// ./myprog deploy myapp production --tail 100
```


### Variadic arguments

You can use `...` to indicate variadic arguments. In that case, the resulted value will be an array.
Expand Down
10 changes: 7 additions & 3 deletions examples/pizza/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies" : {
"caporal": "file:../../"
"dependencies": {
"@types/node": "^11.9.4",
"caporal": "file:../../",
"tsc": "^1.20150623.0"
},
"bin": {
"fly": "./pizza.js"
},
"bin" : { "fly" : "./pizza.js" },
"author": "",
"license": "ISC"
}
17 changes: 17 additions & 0 deletions examples/pizza/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as prog from 'caporal';

prog
.version('1.0.0')
// you specify arguments using .argument()
// 'app' is required, 'env' is optional
.command('ts', 'Basic typescript example')
.argument('[arg]', 'argument desc', /^.*$/, 'default arg')
.option('--option <option>', 'option desc', prog.STRING, 'default option')
.action((args, options, logger) => {
logger.info('Hello');
logger.info(options);
logger.info("%j", args);
});


prog.parse(process.argv);
15 changes: 10 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ type ValidatorArg = string[]|string|RegExp|ValidatorFn|Number;
type ValidatorFn = (str: string) => any;

declare interface Logger {
debug(str: string): void;
info(str: string): void;
log(str: string): void;
warn(str: string): void;
error(str: string): void;
debug(str: string|object): void;
debug(format: string, ...mixed: any[]): void;
info(str: string|object): void;
info(format: string, ...mixed: any[]): void;
log(str: string|object): void;
log(format: string, ...mixed: any[]): void;
warn(str: string|object): void;
warn(format: string, ...mixed: any[]): void;
error(str: string|object): void;
error(format: string, ...mixed: any[]): void;
}

declare interface Command {
Expand Down

0 comments on commit dd0a82e

Please sign in to comment.