Skip to content

Commit 1cf3661

Browse files
committed
Improved parsing of command line arguments.
1 parent d9dc54c commit 1cf3661

File tree

1 file changed

+59
-55
lines changed

1 file changed

+59
-55
lines changed

bin/doxdox

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,87 @@ var input = [],
1717

1818
var args = process.argv.slice(2);
1919

20-
if (args.length) {
20+
var currentArg;
2121

22-
while (args.length && !args[0].match(/^\-/)) {
22+
while (args.length) {
2323

24-
input.push(args.shift());
24+
currentArg = args.shift();
2525

26-
}
26+
if (/^(?!\-).*/.test(currentArg)) {
2727

28-
} else {
28+
input.push(currentArg);
2929

30-
input = process.cwd();
30+
} else {
3131

32-
}
32+
switch (currentArg) {
3333

34-
while (args.length) {
34+
case '-t':
35+
case '--title':
3536

36-
switch (args.shift()) {
37+
config.title = args.shift();
3738

38-
case '-t':
39-
case '--title':
39+
break;
4040

41-
config.title = args.shift();
41+
case '-d':
42+
case '--description':
4243

43-
break;
44+
config.description = args.shift();
4445

45-
case '-d':
46-
case '--description':
46+
break;
4747

48-
config.description = args.shift();
48+
case '-l':
49+
case '--layout':
4950

50-
break;
51+
config.layout = args.shift();
5152

52-
case '-l':
53-
case '--layout':
53+
break;
5454

55-
config.layout = args.shift();
55+
case '-o':
56+
case '--output':
5657

57-
break;
58+
output = args.shift();
5859

59-
case '-o':
60-
case '--output':
60+
break;
6161

62-
output = args.shift();
62+
case '-p':
63+
case '--package':
6364

64-
break;
65+
pkg = args.shift();
6566

66-
case '-p':
67-
case '--package':
67+
break;
6868

69-
pkg = args.shift();
69+
case '-v':
70+
case '--version':
7071

71-
break;
72+
process.stdout.write(require('../package').version + '\n');
73+
process.exit();
7274

73-
case '-v':
74-
case '--version':
75+
break;
7576

76-
process.stdout.write(require('../package').version + '\n');
77-
process.exit();
77+
default:
7878

79-
break;
79+
process.stdout.write('\n');
80+
process.stdout.write(chalk.blue(' Usage:') + ' doxdox <path> [options]' + '\n\n');
81+
process.stdout.write(' Options:' + '\n\n');
82+
process.stdout.write(chalk.yellow(' -h, --help') + '\t\t\tDisplay this help message.' + '\n');
83+
process.stdout.write(chalk.yellow(' -v, --version') + '\t\t\tDisplay the current installed version.' + '\n');
84+
process.stdout.write(chalk.yellow(' -t, --title') + '\t\t\tSets title.' + '\n');
85+
process.stdout.write(chalk.yellow(' -d, --description') + '\t\tSets description.' + '\n');
86+
process.stdout.write(chalk.yellow(' -l, --layout') + '\t\t\tTemplate to render the documentation with.' + '\n');
87+
process.stdout.write(chalk.yellow(' -p, --package') + '\t\t\tSets location of package.json file.' + '\n');
88+
process.stdout.write(chalk.yellow(' -o, --output') + '\t\t\tFile to save documentation to. Defaults to stdout.' + '\n');
89+
process.stdout.write('\n');
90+
process.stdout.write(' Available Layouts:' + '\n\n');
91+
process.stdout.write(' - Bootstrap (default)\t\t(http://getbootstrap.com/)' + '\n');
92+
process.stdout.write(' - Markdown\t\t\t(http://daringfireball.net/projects/markdown/)' + '\n');
93+
process.stdout.write(' - Dash\t\t\t(http://kapeli.com/docsets/)' + '\n');
94+
process.stdout.write(' - Wiki\t\t\t(https://help.github.com/articles/about-github-wikis/)' + '\n');
95+
process.stdout.write('\n');
96+
process.exit();
8097

81-
default:
98+
break;
8299

83-
process.stdout.write('\n');
84-
process.stdout.write(chalk.blue(' Usage:') + ' doxdox <path> [options]' + '\n\n');
85-
process.stdout.write(' Options:' + '\n\n');
86-
process.stdout.write(chalk.yellow(' -h, --help') + '\t\t\tDisplay this help message.' + '\n');
87-
process.stdout.write(chalk.yellow(' -v, --version') + '\t\t\tDisplay the current installed version.' + '\n');
88-
process.stdout.write(chalk.yellow(' -t, --title') + '\t\t\tSets title.' + '\n');
89-
process.stdout.write(chalk.yellow(' -d, --description') + '\t\tSets description.' + '\n');
90-
process.stdout.write(chalk.yellow(' -l, --layout') + '\t\t\tTemplate to render the documentation with.' + '\n');
91-
process.stdout.write(chalk.yellow(' -p, --package') + '\t\t\tSets location of package.json file.' + '\n');
92-
process.stdout.write(chalk.yellow(' -o, --output') + '\t\t\tFile to save documentation to. Defaults to stdout.' + '\n');
93-
process.stdout.write('\n');
94-
process.stdout.write(' Available Layouts:' + '\n\n');
95-
process.stdout.write(' - Bootstrap (default)\t\t(http://getbootstrap.com/)' + '\n');
96-
process.stdout.write(' - Markdown\t\t\t(http://daringfireball.net/projects/markdown/)' + '\n');
97-
process.stdout.write(' - Dash\t\t\t(http://kapeli.com/docsets/)' + '\n');
98-
process.stdout.write(' - Wiki\t\t\t(https://help.github.com/articles/about-github-wikis/)' + '\n');
99-
process.stdout.write('\n');
100-
process.exit();
101-
102-
break;
100+
}
103101

104102
}
105103

@@ -139,6 +137,12 @@ if (!config.title) {
139137

140138
}
141139

140+
if (!input.length) {
141+
142+
input = process.cwd();
143+
144+
}
145+
142146
doxdox.parseInput(input, config, pkg).then(function (content) {
143147

144148
if (output) {

0 commit comments

Comments
 (0)