Skip to content
This repository was archived by the owner on Feb 24, 2024. It is now read-only.

Commit 62f5353

Browse files
committed
Add editorconfig and reformat files
1 parent 0c4cb65 commit 62f5353

18 files changed

+922
-911
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
end_of_line = lf
9+
10+
[*.{ts,js}]
11+
indent_size = 2

src/cli.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
#!/usr/bin/env node
2-
import {EOL} from "os";
3-
import {argv, exit, stderr} from "process";
2+
import { EOL } from "os";
3+
import { argv, exit, stderr } from "process";
44
import Cli from "./cli/Cli";
5-
import CliError, {ErrorCode} from "./cli/CliError";
6-
import {formatErrorStack} from "./common/errors";
5+
import CliError, { ErrorCode } from "./cli/CliError";
6+
import { formatErrorStack } from "./common/errors";
77

88
const unexpectedErrorMessage = `Sorry, an unexpected error has occurred :-(
99
It would be great if you spend few minutes and report it at:
10-
10+
1111
https://github.com/RandomVoid/cpp-merge/issues/new
12-
12+
1313
Please attach following information:
1414
`;
1515

1616
const cli = new Cli();
1717

1818
try {
19-
const args = argv.slice(2);
20-
cli.run(args);
19+
const args = argv.slice(2);
20+
cli.run(args);
2121
} catch (error) {
22-
if (error instanceof CliError) {
23-
stderr.write(error.message);
24-
stderr.write(EOL);
25-
exit(error.errorCode);
26-
}
27-
stderr.write(unexpectedErrorMessage);
28-
stderr.write(formatErrorStack(error));
22+
if (error instanceof CliError) {
23+
stderr.write(error.message);
2924
stderr.write(EOL);
30-
exit(ErrorCode.UnexpectedError);
25+
exit(error.errorCode);
26+
}
27+
stderr.write(unexpectedErrorMessage);
28+
stderr.write(formatErrorStack(error));
29+
stderr.write(EOL);
30+
exit(ErrorCode.UnexpectedError);
3131
}

src/cli/ArgumentParser.ts

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,126 @@
11
import assert from "assert";
2-
import {TraceError} from "../common/errors";
2+
import { TraceError } from "../common/errors";
33
import HelpFormatter from "./HelpFormatter";
4-
import {Argument, Option} from "./types";
4+
import { Argument, Option } from "./types";
55

66
export class ArgumentError extends TraceError {
7-
public readonly argument: string;
7+
public readonly argument: string;
88

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+
}
1313
}
1414

1515
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+
}
1919
}
2020

2121
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+
}
2525
}
2626

2727
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+
}
3131
}
3232

3333
export type ParseResult = {
34-
arguments: ParsedArguments;
35-
options: ParsedOptions;
34+
arguments: ParsedArguments;
35+
options: ParsedOptions;
3636
};
3737

3838
type ParsedArguments = {
39-
[name: string]: string | undefined;
39+
[name: string]: string | undefined;
4040
};
4141

4242
type ParsedOptions = {
43-
[name: string]: string | undefined;
43+
[name: string]: string | undefined;
4444
};
4545

4646
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;
6088
}
6189

62-
public addOption(option: Option): void {
63-
this.options.push(option);
64-
}
90+
return {
91+
arguments: parsedArguments,
92+
options: parsedOptions
93+
};
94+
}
6595

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+
}
9999

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);
115104
}
116105

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: "" };
121108
}
122109

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);
125113
}
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+
}
126126
}

0 commit comments

Comments
 (0)