Merge pull request #672 from quicktype/fix-default-on-options

Invert boolean options that are on by default on CLI. Fixes #671
This commit is contained in:
Mark Probst 2018-03-14 15:00:06 -07:00 коммит произвёл GitHub
Родитель 0c3cf1f406 cc141ff2f6
Коммит 26d1eef068
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 43 добавлений и 8 удалений

Просмотреть файл

@ -49,7 +49,8 @@ export default class SwiftTargetLanguage extends TargetLanguage {
private readonly _convenienceInitializers = new BooleanOption(
"initializers",
"Convenience initializers",
true
true,
"No convenience initializers"
);
private readonly _alamofireHandlers = new BooleanOption("alamofire", "Alamofire extensions", false);

Просмотреть файл

@ -1,6 +1,6 @@
"use strict";
import { panic } from "./Support";
import { panic, assert } from "./Support";
export interface OptionDefinition {
name: string;
@ -22,6 +22,10 @@ export abstract class UntypedOption {
definition.renderer = true;
this.definition = definition;
}
get cliDefinition(): OptionDefinition {
return this.definition;
}
}
export abstract class Option<T> extends UntypedOption {
@ -35,14 +39,40 @@ export abstract class Option<T> extends UntypedOption {
}
export class BooleanOption extends Option<boolean> {
constructor(name: string, description: string, defaultValue: boolean) {
const definition = {
constructor(name: string, description: string, defaultValue: boolean, private readonly _cliDescription?: string) {
super({
name,
type: Boolean,
description,
defaultValue
};
super(definition);
});
if (defaultValue === true) {
assert(_cliDescription !== undefined, "We need a CLI description for boolean options that are true by default");
}
}
get cliDefinition(): OptionDefinition {
const definition = Object.assign({}, this.definition);
if (this._cliDescription !== undefined) {
definition.description = this._cliDescription;
}
if (this.definition.defaultValue === true) {
definition.name = `no-${definition.name}`;
}
definition.defaultValue = false;
return definition;
}
getValue(values: { [name: string]: any }): boolean {
const definition = this.cliDefinition;
const value = values[definition.name];
if (value === undefined) {
return this.definition.defaultValue;
}
if (this.definition.defaultValue === true) {
return !value;
}
return value;
}
}

Просмотреть файл

@ -19,6 +19,10 @@ export abstract class TargetLanguage {
return this.getOptions().map(o => o.definition);
}
get cliOptionDefinitions(): OptionDefinition[] {
return this.getOptions().map(o => o.cliDefinition);
}
protected abstract get rendererClass(): new (
graph: TypeGraph,
leadingComments: string[] | undefined,

Просмотреть файл

@ -450,7 +450,7 @@ const sectionsAfterRenderers: UsageSection[] = [
];
function getOptionDefinitions(opts: CLIOptions): OptionDefinition[] {
return getTargetLanguage(opts.lang).optionDefinitions;
return getTargetLanguage(opts.lang).cliOptionDefinitions;
}
function parseArgv(argv: string[]): CLIOptions {
@ -501,7 +501,7 @@ function usage() {
const rendererSections: UsageSection[] = [];
_.forEach(targetLanguages.all, language => {
const definitions = language.optionDefinitions;
const definitions = language.cliOptionDefinitions;
if (definitions.length === 0) return;
rendererSections.push({