Skip to content

Commit 1c0064e

Browse files
authored
use Node parseArgs for parsing CLI arguments (#117)
* breaking: use Node `parseArgs` for parsing CLI arguments * lol, is was not breaking
1 parent c3bbfe6 commit 1c0064e

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/program.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
import { analyze } from '@projectwallace/css-analyzer'
2+
import { parseArgs } from 'node:util'
23
import { help } from './help.js'
34
import { Analytics } from './components.js'
45

56
export async function Program({ args, read_file, terminal_colors, stdin }) {
6-
const format_as_json = '--json'
7-
const help_args = ['-h', '--help']
7+
const options = {
8+
json: {
9+
type: 'boolean',
10+
short: 'j'
11+
},
12+
help: {
13+
type: 'boolean',
14+
short: 'h'
15+
}
16+
}
17+
18+
const { values, positionals } = parseArgs({
19+
args,
20+
options,
21+
allowPositionals: true,
22+
strict: false
23+
})
824

9-
// Show help if the user explicitly asked for it
10-
if (args.some(arg => help_args.includes(arg))
11-
// Show help if there's no input and no arguments provided
12-
|| args.length === 0 && stdin === '') {
25+
// Show help if the user explicitly asked for it or if no arguments were provided
26+
if (values.help || args.length === 0 && stdin === '') {
1327
return help(terminal_colors)
1428
}
1529

16-
// path is the first param that doesn't start with -- and isn't one
17-
// of the existing flags
18-
const path_param = args.find(arg => {
19-
if (arg == format_as_json) return false
20-
if (help_args.includes(arg)) return false
21-
if (arg.startsWith('--')) return false
22-
return true
23-
})
30+
// Use the first positional argument as the file path
31+
const path_param = positionals[0]
2432
const css = path_param ? await read_file(path_param) : stdin
2533

2634
if (!css) {
@@ -31,7 +39,7 @@ export async function Program({ args, read_file, terminal_colors, stdin }) {
3139
delete stats.__meta__
3240

3341
// Format as JSON if user asked for it
34-
if (args.some(arg => arg === format_as_json)) {
42+
if (values.json) {
3543
return JSON.stringify(stats)
3644
}
3745

0 commit comments

Comments
 (0)