UNPKG

2.81 kBMarkdownView Raw
1@oclif/command
2===============
3
4oclif base command
5
6[![Version](https://img.shields.io/npm/v/@oclif/command.svg)](https://npmjs.org/package/@oclif/command)
7[![CircleCI](https://circleci.com/gh/oclif/command/tree/master.svg?style=shield)](https://circleci.com/gh/oclif/command/tree/master)
8[![Appveyor CI](https://ci.appveyor.com/api/projects/status/github/oclif/command?branch=master&svg=true)](https://ci.appveyor.com/project/heroku/command/branch/master)
9[![Codecov](https://codecov.io/gh/oclif/command/branch/master/graph/badge.svg)](https://codecov.io/gh/oclif/command)
10[![Known Vulnerabilities](https://snyk.io/test/npm/@oclif/command/badge.svg)](https://snyk.io/test/npm/@oclif/command)
11[![Downloads/week](https://img.shields.io/npm/dw/@oclif/command.svg)](https://npmjs.org/package/@oclif/command)
12[![License](https://img.shields.io/npm/l/@oclif/command.svg)](https://github.com/oclif/command/blob/master/package.json)
13
14This is about half of the main codebase for oclif. The other half lives in [@oclif/config](https://github.com/oclif/config). This can be used directly, but it probably makes more sense to build your CLI with the [generator](https://github.com/oclif/oclif).
15
16Usage
17=====
18
19Without the generator, you can create a simple CLI like this:
20
21**TypeScript**
22```js
23#!/usr/bin/env ts-node
24
25import * as fs from 'fs'
26import {Command, flags} from '@oclif/command'
27
28class LS extends Command {
29 static flags = {
30 version: flags.version(),
31 help: flags.help(),
32 // run with --dir= or -d=
33 dir: flags.string({
34 char: 'd',
35 default: process.cwd(),
36 }),
37 }
38
39 async run() {
40 const {flags} = this.parse(LS)
41 let files = fs.readdirSync(flags.dir)
42 for (let f of files) {
43 this.log(f)
44 }
45 }
46}
47
48LS.run()
49.catch(require('@oclif/errors/handle'))
50```
51
52**JavaScript**
53```js
54#!/usr/bin/env node
55
56const fs = require('fs')
57const {Command, flags} = require('@oclif/command')
58
59class LS extends Command {
60 async run() {
61 const {flags} = this.parse(LS)
62 let files = fs.readdirSync(flags.dir)
63 for (let f of files) {
64 this.log(f)
65 }
66 }
67}
68
69LS.flags = {
70 version: flags.version(),
71 help: flags.help(),
72 // run with --dir= or -d=
73 dir: flags.string({
74 char: 'd',
75 default: process.cwd(),
76 }),
77}
78
79LS.run()
80.catch(require('@oclif/errors/handle'))
81```
82
83Then run either of these with:
84
85```sh-session
86$ ./myscript
87...files in current dir...
88$ ./myscript --dir foobar
89...files in ./foobar...
90$ ./myscript --version
91myscript/0.0.0 darwin-x64 node-v9.5.0
92$ ./myscript --help
93USAGE
94 $ @oclif/command
95
96OPTIONS
97 -d, --dir=dir [default: /Users/jdickey/src/github.com/oclif/command]
98 --help show CLI help
99 --version show CLI version
100```
101
102See the [generator](https://github.com/oclif/oclif) for all the options you can pass to the command.