UNPKG

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