|
1 | 1 | import assert from "assert"; |
2 | | -import {TraceError} from "../common/errors"; |
| 2 | +import { TraceError } from "../common/errors"; |
3 | 3 | import HelpFormatter from "./HelpFormatter"; |
4 | | -import {Argument, Option} from "./types"; |
| 4 | +import { Argument, Option } from "./types"; |
5 | 5 |
|
6 | 6 | export class ArgumentError extends TraceError { |
7 | | - public readonly argument: string; |
| 7 | + public readonly argument: string; |
8 | 8 |
|
9 | | - public constructor(message: string, argument: string) { |
10 | | - super(message); |
11 | | - this.argument = argument; |
12 | | - } |
| 9 | + public constructor(message: string, argument: string) { |
| 10 | + super(message); |
| 11 | + this.argument = argument; |
| 12 | + } |
13 | 13 | } |
14 | 14 |
|
15 | 15 | export class UnknownArgumentError extends ArgumentError { |
16 | | - public constructor(argument: string, message = `Unknown argument '${argument}'`) { |
17 | | - super(message, argument); |
18 | | - } |
| 16 | + public constructor(argument: string, message = `Unknown argument '${argument}'`) { |
| 17 | + super(message, argument); |
| 18 | + } |
19 | 19 | } |
20 | 20 |
|
21 | 21 | export class UnknownOptionError extends ArgumentError { |
22 | | - public constructor(argument: string, message = `Unknown option '${argument}'`) { |
23 | | - super(message, argument); |
24 | | - } |
| 22 | + public constructor(argument: string, message = `Unknown option '${argument}'`) { |
| 23 | + super(message, argument); |
| 24 | + } |
25 | 25 | } |
26 | 26 |
|
27 | 27 | export class OptionArgumentExpectedError extends ArgumentError { |
28 | | - public constructor(option: string, message = `Option '${option}' requires a value`) { |
29 | | - super(message, option); |
30 | | - } |
| 28 | + public constructor(option: string, message = `Option '${option}' requires a value`) { |
| 29 | + super(message, option); |
| 30 | + } |
31 | 31 | } |
32 | 32 |
|
33 | 33 | export type ParseResult = { |
34 | | - arguments: ParsedArguments; |
35 | | - options: ParsedOptions; |
| 34 | + arguments: ParsedArguments; |
| 35 | + options: ParsedOptions; |
36 | 36 | }; |
37 | 37 |
|
38 | 38 | type ParsedArguments = { |
39 | | - [name: string]: string | undefined; |
| 39 | + [name: string]: string | undefined; |
40 | 40 | }; |
41 | 41 |
|
42 | 42 | type ParsedOptions = { |
43 | | - [name: string]: string | undefined; |
| 43 | + [name: string]: string | undefined; |
44 | 44 | }; |
45 | 45 |
|
46 | 46 | export default class ArgumentParser { |
47 | | - private readonly helpFormatter = new HelpFormatter(); |
48 | | - private readonly arguments: Argument[] = []; |
49 | | - private readonly options: Option[] = []; |
50 | | - private readonly programName; |
51 | | - private readonly description; |
52 | | - |
53 | | - public constructor(params: {programName: string, description: string}) { |
54 | | - this.programName = params.programName; |
55 | | - this.description = params.description; |
56 | | - } |
57 | | - |
58 | | - public addArgument(argument: Argument): void { |
59 | | - this.arguments.push(argument); |
| 47 | + private readonly helpFormatter = new HelpFormatter(); |
| 48 | + private readonly arguments: Argument[] = []; |
| 49 | + private readonly options: Option[] = []; |
| 50 | + private readonly programName; |
| 51 | + private readonly description; |
| 52 | + |
| 53 | + public constructor(params: { programName: string, description: string }) { |
| 54 | + this.programName = params.programName; |
| 55 | + this.description = params.description; |
| 56 | + } |
| 57 | + |
| 58 | + public addArgument(argument: Argument): void { |
| 59 | + this.arguments.push(argument); |
| 60 | + } |
| 61 | + |
| 62 | + public addOption(option: Option): void { |
| 63 | + this.options.push(option); |
| 64 | + } |
| 65 | + |
| 66 | + public parseArguments(args: string[]): ParseResult { |
| 67 | + const argumentsToParse = [...args]; |
| 68 | + const parsedArguments: ParsedArguments = {}; |
| 69 | + const parsedOptions: ParsedOptions = {}; |
| 70 | + let argumentIndex = 0; |
| 71 | + |
| 72 | + while (argumentsToParse.length > 0) { |
| 73 | + const argument = argumentsToParse.shift(); |
| 74 | + assert(argument); |
| 75 | + if (this.isOption(argument)) { |
| 76 | + const { name, value } = this.parseOption(argument, argumentsToParse); |
| 77 | + parsedOptions[name] = value; |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + if (argumentIndex >= this.arguments.length) { |
| 82 | + throw new UnknownArgumentError(argument); |
| 83 | + } |
| 84 | + |
| 85 | + const argumentName = this.arguments[argumentIndex].name; |
| 86 | + parsedArguments[argumentName] = argument; |
| 87 | + ++argumentIndex; |
60 | 88 | } |
61 | 89 |
|
62 | | - public addOption(option: Option): void { |
63 | | - this.options.push(option); |
64 | | - } |
| 90 | + return { |
| 91 | + arguments: parsedArguments, |
| 92 | + options: parsedOptions |
| 93 | + }; |
| 94 | + } |
65 | 95 |
|
66 | | - public parseArguments(args: string[]): ParseResult { |
67 | | - const argumentsToParse = [...args]; |
68 | | - const parsedArguments: ParsedArguments = {}; |
69 | | - const parsedOptions: ParsedOptions = {}; |
70 | | - let argumentIndex = 0; |
71 | | - |
72 | | - while (argumentsToParse.length > 0) { |
73 | | - const argument = argumentsToParse.shift(); |
74 | | - assert(argument); |
75 | | - if (this.isOption(argument)) { |
76 | | - const {name, value} = this.parseOption(argument, argumentsToParse); |
77 | | - parsedOptions[name] = value; |
78 | | - continue; |
79 | | - } |
80 | | - |
81 | | - if (argumentIndex >= this.arguments.length) { |
82 | | - throw new UnknownArgumentError(argument); |
83 | | - } |
84 | | - |
85 | | - const argumentName = this.arguments[argumentIndex].name; |
86 | | - parsedArguments[argumentName] = argument; |
87 | | - ++argumentIndex; |
88 | | - } |
89 | | - |
90 | | - return { |
91 | | - arguments: parsedArguments, |
92 | | - options: parsedOptions |
93 | | - }; |
94 | | - } |
95 | | - |
96 | | - private isOption(argument: string) { |
97 | | - return argument.startsWith("-"); |
98 | | - } |
| 96 | + private isOption(argument: string) { |
| 97 | + return argument.startsWith("-"); |
| 98 | + } |
99 | 99 |
|
100 | | - private parseOption(argument: string, args: string[]): {name: string, value: string} { |
101 | | - const option = this.findOption(argument); |
102 | | - if (!option) { |
103 | | - throw new UnknownOptionError(argument); |
104 | | - } |
105 | | - |
106 | | - if (!option.value) { |
107 | | - return {name: option.name, value: ""}; |
108 | | - } |
109 | | - |
110 | | - const value = args.shift(); |
111 | | - if (!value) { |
112 | | - throw new OptionArgumentExpectedError(argument); |
113 | | - } |
114 | | - return {name: option.name, value: value}; |
| 100 | + private parseOption(argument: string, args: string[]): { name: string, value: string } { |
| 101 | + const option = this.findOption(argument); |
| 102 | + if (!option) { |
| 103 | + throw new UnknownOptionError(argument); |
115 | 104 | } |
116 | 105 |
|
117 | | - private findOption(option: string): Option | undefined { |
118 | | - return this.options.find((opt: Option) => { |
119 | | - return opt.options.find(o => o === option); |
120 | | - }); |
| 106 | + if (!option.value) { |
| 107 | + return { name: option.name, value: "" }; |
121 | 108 | } |
122 | 109 |
|
123 | | - public formatHelp(): string { |
124 | | - return this.helpFormatter.formatHelp(this.programName, this.description, this.arguments, this.options); |
| 110 | + const value = args.shift(); |
| 111 | + if (!value) { |
| 112 | + throw new OptionArgumentExpectedError(argument); |
125 | 113 | } |
| 114 | + return { name: option.name, value: value }; |
| 115 | + } |
| 116 | + |
| 117 | + private findOption(option: string): Option | undefined { |
| 118 | + return this.options.find((opt: Option) => { |
| 119 | + return opt.options.find(o => o === option); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + public formatHelp(): string { |
| 124 | + return this.helpFormatter.formatHelp(this.programName, this.description, this.arguments, this.options); |
| 125 | + } |
126 | 126 | } |
0 commit comments