@guanghechen/commander 1.0.11 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +147 -17
- package/lib/cjs/index.cjs +842 -145
- package/lib/esm/index.mjs +838 -131
- package/lib/types/index.d.ts +199 -119
- package/package.json +18 -19
- package/CHANGELOG.md +0 -106
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2023-present guanghechen (https://github.com/guanghechen)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<header>
|
|
2
2
|
<h1 align="center">
|
|
3
|
-
<a href="https://github.com/guanghechen/
|
|
3
|
+
<a href="https://github.com/guanghechen/sora/tree/@guanghechen/commander@1.0.0/packages/commander#readme">@guanghechen/commander</a>
|
|
4
4
|
</h1>
|
|
5
5
|
<div align="center">
|
|
6
6
|
<a href="https://www.npmjs.com/package/@guanghechen/commander">
|
|
@@ -33,12 +33,6 @@
|
|
|
33
33
|
src="https://img.shields.io/node/v/@guanghechen/commander"
|
|
34
34
|
/>
|
|
35
35
|
</a>
|
|
36
|
-
<a href="https://github.com/facebook/jest">
|
|
37
|
-
<img
|
|
38
|
-
alt="Eslint Version"
|
|
39
|
-
src="https://img.shields.io/npm/dependency-version/@guanghechen/commander/peer/jest"
|
|
40
|
-
/>
|
|
41
|
-
</a>
|
|
42
36
|
<a href="https://github.com/facebook/jest">
|
|
43
37
|
<img
|
|
44
38
|
alt="Tested with Jest"
|
|
@@ -55,29 +49,165 @@
|
|
|
55
49
|
</header>
|
|
56
50
|
<br/>
|
|
57
51
|
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
A minimal, type-safe command-line interface builder with fluent API. Supports subcommands, option
|
|
53
|
+
parsing, shell completion generation (bash, fish, pwsh), and built-in help/version handling.
|
|
60
54
|
|
|
61
55
|
## Install
|
|
62
56
|
|
|
63
|
-
|
|
57
|
+
- npm
|
|
64
58
|
|
|
65
59
|
```bash
|
|
66
|
-
npm install --save
|
|
60
|
+
npm install --save @guanghechen/commander
|
|
67
61
|
```
|
|
68
62
|
|
|
69
|
-
|
|
63
|
+
- yarn
|
|
70
64
|
|
|
71
65
|
```bash
|
|
72
|
-
yarn add
|
|
66
|
+
yarn add @guanghechen/commander
|
|
73
67
|
```
|
|
74
68
|
|
|
69
|
+
## Usage
|
|
70
|
+
|
|
71
|
+
### Basic Command
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { Command } from '@guanghechen/commander'
|
|
75
|
+
|
|
76
|
+
const cli = new Command({
|
|
77
|
+
name: 'mycli',
|
|
78
|
+
version: '1.0.0',
|
|
79
|
+
description: 'My awesome CLI tool',
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
cli
|
|
83
|
+
.option({
|
|
84
|
+
long: 'verbose',
|
|
85
|
+
short: 'v',
|
|
86
|
+
type: 'boolean',
|
|
87
|
+
description: 'Enable verbose output',
|
|
88
|
+
})
|
|
89
|
+
.option({
|
|
90
|
+
long: 'output',
|
|
91
|
+
short: 'o',
|
|
92
|
+
type: 'string',
|
|
93
|
+
description: 'Output file path',
|
|
94
|
+
default: './output.txt',
|
|
95
|
+
})
|
|
96
|
+
.argument({
|
|
97
|
+
name: 'file',
|
|
98
|
+
kind: 'required',
|
|
99
|
+
description: 'Input file to process',
|
|
100
|
+
})
|
|
101
|
+
.action(({ opts, args, ctx }) => {
|
|
102
|
+
const [file] = args
|
|
103
|
+
ctx.reporter.info(`Processing ${file}...`)
|
|
104
|
+
if (opts['verbose']) {
|
|
105
|
+
ctx.reporter.debug(`Output: ${opts['output']}`)
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
cli.run({
|
|
110
|
+
argv: process.argv.slice(2),
|
|
111
|
+
envs: process.env,
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Subcommands
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { Command } from '@guanghechen/commander'
|
|
119
|
+
|
|
120
|
+
const root = new Command({
|
|
121
|
+
name: 'git',
|
|
122
|
+
version: '1.0.0',
|
|
123
|
+
description: 'A simple git-like CLI',
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
const clone = new Command({
|
|
127
|
+
name: 'clone',
|
|
128
|
+
description: 'Clone a repository',
|
|
129
|
+
})
|
|
130
|
+
.argument({ name: 'url', kind: 'required', description: 'Repository URL' })
|
|
131
|
+
.option({ long: 'depth', type: 'number', description: 'Shallow clone depth' })
|
|
132
|
+
.action(({ args, opts }) => {
|
|
133
|
+
console.log(`Cloning ${args[0]} with depth ${opts['depth'] ?? 'full'}`)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const commit = new Command({
|
|
137
|
+
name: 'commit',
|
|
138
|
+
aliases: ['ci'],
|
|
139
|
+
description: 'Record changes to the repository',
|
|
140
|
+
})
|
|
141
|
+
.option({ long: 'message', short: 'm', type: 'string', required: true, description: 'Commit message' })
|
|
142
|
+
.option({ long: 'amend', type: 'boolean', description: 'Amend previous commit' })
|
|
143
|
+
.action(({ opts }) => {
|
|
144
|
+
console.log(`Committing: ${opts['message']}`)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
root.subcommand(clone).subcommand(commit)
|
|
148
|
+
|
|
149
|
+
root.run({ argv: process.argv.slice(2), envs: process.env })
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Shell Completion
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import { Command, CompletionCommand } from '@guanghechen/commander'
|
|
156
|
+
|
|
157
|
+
const root = new Command({
|
|
158
|
+
name: 'mycli',
|
|
159
|
+
version: '1.0.0',
|
|
160
|
+
description: 'My CLI with completion support',
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
// Add completion subcommand
|
|
164
|
+
root.subcommand(new CompletionCommand(root))
|
|
165
|
+
|
|
166
|
+
// Generate completion scripts:
|
|
167
|
+
// mycli completion --bash > ~/.local/share/bash-completion/completions/mycli
|
|
168
|
+
// mycli completion --fish > ~/.config/fish/completions/mycli.fish
|
|
169
|
+
// mycli completion --pwsh >> $PROFILE
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Option Types
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
import { Command } from '@guanghechen/commander'
|
|
176
|
+
|
|
177
|
+
new Command({ name: 'example', description: 'Option types demo' })
|
|
178
|
+
// Boolean (flags)
|
|
179
|
+
.option({ long: 'debug', type: 'boolean', description: 'Enable debug mode' })
|
|
180
|
+
|
|
181
|
+
// String with choices
|
|
182
|
+
.option({
|
|
183
|
+
long: 'format',
|
|
184
|
+
type: 'string',
|
|
185
|
+
choices: ['json', 'yaml', 'toml'],
|
|
186
|
+
default: 'json',
|
|
187
|
+
description: 'Output format'
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
// Number
|
|
191
|
+
.option({ long: 'port', type: 'number', default: 3000, description: 'Server port' })
|
|
192
|
+
|
|
193
|
+
// Array (can be specified multiple times)
|
|
194
|
+
.option({ long: 'include', type: 'string[]', description: 'Files to include' })
|
|
75
195
|
|
|
76
|
-
|
|
196
|
+
// Required option
|
|
197
|
+
.option({ long: 'config', type: 'string', required: true, description: 'Config file' })
|
|
77
198
|
|
|
78
|
-
|
|
199
|
+
// Custom coercion
|
|
200
|
+
.option({
|
|
201
|
+
long: 'date',
|
|
202
|
+
type: 'string',
|
|
203
|
+
coerce: (value) => new Date(value),
|
|
204
|
+
description: 'Date value',
|
|
205
|
+
})
|
|
206
|
+
```
|
|
79
207
|
|
|
208
|
+
## Reference
|
|
80
209
|
|
|
81
|
-
[homepage]
|
|
82
|
-
[commander.js]: https://github.com/tj/commander.js/
|
|
210
|
+
- [homepage][homepage]
|
|
83
211
|
|
|
212
|
+
[homepage]:
|
|
213
|
+
https://github.com/guanghechen/sora/tree/@guanghechen/commander@1.0.0/packages/commander#readme
|