-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·78 lines (67 loc) · 2.41 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
const { Command, InvalidOptionArgumentError } = require('commander')
const program = new Command()
const { inspectCommand } = require('./commands/inspect')
const { readCommand } = require('./commands/read')
const { writeCommand } = require('./commands/write')
const { clearCommand } = require('./commands/clear')
const { executeCommand } = require('./shared/utils')
program
.version('v'+require('./package.json').version, '-v, --version', 'output the current version')
// global options
program
.option('-d, --debug', 'enable debug mode', false)
// subcommand: inspect <vhd>
program
.command('inspect <vhd>')
.description('inspect virtual disk file structure', {
vhd: 'virtual hard disk file'
})
.action((vhd, options, command) => {
executeCommand(() => inspectCommand(vhd), program.opts().debug)
})
// subcommand: read <vhd>
program
.command('read <vhd>')
.description('read sector data from virtual disk file', {
vhd: 'virtual hard disk file'
})
.option('-s, --sector <sector>', 'sector number to read begin', optionParseInt, 0)
.option('-c, --count <count>', 'sector count to read', optionParseInt, 1)
.action((vhd, { sector, count }) => {
// todo: 这里可以添加格式化器
executeCommand(() => console.log(readCommand(vhd, sector, count)), program.opts().debug)
})
// subcommand: write <vhd> <bin>
program
.command('write <vhd> <bin>')
.description('write special binary file to virtual disk file', {
vhd: 'virtual hard disk file',
bin: 'binary file'
})
.option('-s, --sector <sector>', 'sector number to write begin', optionParseInt, 0)
.action((vhd, bin, { sector }) => {
executeCommand(() => writeCommand(vhd, bin, sector), program.opts().debug)
})
// subcommand: clear <vhd>
program
.command('clear <vhd>')
.description('clear virtual disk file content', {
vhd: 'virtual hard disk file'
})
.action((vhd) => {
executeCommand(() => clearCommand(vhd), program.opts().debug)
})
// subcommand: graph <vhd>
// program
// .command('graph')
// .description('generate graph about vhd structure')
program.parse(process.argv)
// option parser
function optionParseInt(value) {
const parsedValue = parseInt(value, 10)
if (isNaN(parsedValue)) {
throw new InvalidOptionArgumentError('Not a number.')
}
return parsedValue
}