1 | @oclif/command
|
2 | ===============
|
3 |
|
4 | oclif 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 |
|
14 | This 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 |
|
16 | Usage
|
17 | =====
|
18 |
|
19 | Without the generator, you can create a simple CLI like this:
|
20 |
|
21 | **TypeScript**
|
22 | ```js
|
23 | #!/usr/bin/env ts-node
|
24 |
|
25 | import * as fs from 'fs'
|
26 | import {Command, flags} from '@oclif/command'
|
27 |
|
28 | class 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 |
|
48 | LS.run()
|
49 | .catch(require('@oclif/errors/handle'))
|
50 | ```
|
51 |
|
52 | **JavaScript**
|
53 | ```js
|
54 | #!/usr/bin/env node
|
55 |
|
56 | const fs = require('fs')
|
57 | const {Command, flags} = require('@oclif/command')
|
58 |
|
59 | class 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 |
|
69 | LS.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 |
|
79 | LS.run()
|
80 | .catch(require('@oclif/errors/handle'))
|
81 | ```
|
82 |
|
83 | Then 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
|
91 | myscript/0.0.0 darwin-x64 node-v9.5.0
|
92 | $ ./myscript --help
|
93 | USAGE
|
94 | $ @oclif/command
|
95 |
|
96 | OPTIONS
|
97 | -d, --dir=dir [default: /Users/jdickey/src/github.com/oclif/command]
|
98 | --help show CLI help
|
99 | --version show CLI version
|
100 | ```
|
101 |
|
102 | See the [generator](https://github.com/oclif/oclif) for all the options you can pass to the command.
|