Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type ParseResult<T> =
{ isValid: true; result: T }
| { isValid: false, errorMessage: string};

type ArgumentParseResult<T> =
{ isValid: true; result: T; increment?: number }
| { isValid: false, errorMessage: string };

interface ParsedCommandLine extends ts.ParsedCommandLine {
options: CompilerOptions;
}
Expand Down Expand Up @@ -190,10 +194,11 @@ function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CL
const argumentName = args[i].substr(2);
const option = optionDeclarations[argumentName];
if (option) {
const argumentResult = getArgumentValue(argumentName, args[i + 1]);
i++; // Skip the value from being considered as argument name
const argumentResult = getArgumentValue(argumentName, i, args);
if (argumentResult.isValid === true) {
result[argumentName] = argumentResult.result;
// Skip value from being considered as option
i += argumentResult.increment !== undefined ? argumentResult.increment : 1;
} else {
return { isValid: false, errorMessage: argumentResult.errorMessage };
}
Expand All @@ -209,10 +214,11 @@ function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CL
}

if (argumentName) {
const argumentResult = getArgumentValue(argumentName, args[i + 1]);
i++; // Skip the value from being considered as argument name
const argumentResult = getArgumentValue(argumentName, i, args);
if (argumentResult.isValid === true) {
result[argumentName] = argumentResult.result;
// Skip value from being considered as option
i += argumentResult.increment !== undefined ? argumentResult.increment : 1;
} else {
return { isValid: false, errorMessage: argumentResult.errorMessage };
}
Expand All @@ -232,13 +238,24 @@ function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CL
return { isValid: true, result: commandLine };
}

function getArgumentValue(argumentName: string, argument: string): ParseResult<string | boolean>
function getArgumentValue(
argumentName: string,
argumentIndex: number,
args: string[]
): ArgumentParseResult<string | boolean>
{
const option = optionDeclarations[argumentName];
const argument = args[argumentIndex + 1];

if (option.type === "boolean" && (argument === undefined || argument.startsWith("-"))) {
// Set boolean arguments without supplied value to true
return { isValid: true, result: true, increment: 0 };
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better for result to be option.default here (in case a flag option is added that defaults to true if not specified)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be !option.default? Otherwise setting the option without value would be the same as not setting it at all. I'm a little concerned that's just confusing. Would it ever make sense to have an option that is true by default and can be set to false by just adding its name to CLI? I'm not sure that is a valid use case.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, actually you're probably right. If you set a flag up like that, it could just be inverted.

}

if (argument === undefined) {
return { isValid: false, errorMessage: `Missing value for parameter ${argumentName}`};
}

const option = optionDeclarations[argumentName];
const value = readValue(argument, option.type, argumentName);

if (option.choices) {
Expand Down
32 changes: 32 additions & 0 deletions test/unit/commandLineParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export class CommandLineParserTests
@TestCase([""], false)
@TestCase(["--noHeader", "true"], true)
@TestCase(["--noHeader", "false"], false)
@TestCase(["--noHeader"], true)
@TestCase(["--noHeader", "--noHoisting"], true)
@Test("CLI parser noHeader")
public cliParserNoHeader(args: string[], expected: boolean): void {
const result = parseCommandLine(args);
Expand All @@ -76,6 +78,21 @@ export class CommandLineParserTests
}
}

@TestCase([""], false)
@TestCase(["--noHoisting", "true"], true)
@TestCase(["--noHoisting", "false"], false)
@TestCase(["--noHoisting"], true)
@TestCase(["--noHoisting", "--noHeader"], true)
@Test("CLI parser noHoisting")
public cliParserNoHoisting(args: string[], expected: boolean): void {
const result = parseCommandLine(args);
if (result.isValid === true) {
Expect(result.result.options.noHoisting).toBe(expected);
} else {
Expect(result.isValid).toBeTruthy();
}
}

@TestCase([""], false)
@TestCase(["--project", "tsconfig.json"], true)
@TestCase(["-p", "tsconfig.json"], true)
Expand All @@ -89,6 +106,21 @@ export class CommandLineParserTests
}
}

@Test("CLI Parser Multiple Options")
public cliParserMultipleOptions(): void {
const commandLine = "--project tsconfig.json --noHeader --noHoisting -lt 5.3";
const result = parseCommandLine(commandLine.split(" "));

if (result.isValid === true) {
Expect(result.result.options.project).toBeDefined();
Expect(result.result.options.noHeader).toBe(true);
Expect(result.result.options.noHoisting).toBe(true);
Expect(result.result.options.luaTarget).toBe(LuaTarget.Lua53);
} else {
Expect(result.isValid).toBeTruthy();
}
}

@TestCase([""], false)
@TestCase(["--help"], true)
@TestCase(["-h"], true)
Expand Down