Skip to content

Commit

Permalink
Return the required variants when the 'defaultValue' option is provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
iclanton committed Mar 2, 2024
1 parent 3ae75c2 commit 3d704d8
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 24 deletions.
7 changes: 5 additions & 2 deletions apps/api-documenter/src/cli/YamlAction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import type { CommandLineFlagParameter, CommandLineChoiceParameter } from '@rushstack/ts-command-line';
import type {
CommandLineFlagParameter,
IRequiredCommandLineChoiceParameter
} from '@rushstack/ts-command-line';

import type { ApiDocumenterCommandLine } from './ApiDocumenterCommandLine';
import { BaseAction } from './BaseAction';
Expand All @@ -12,7 +15,7 @@ import { OfficeYamlDocumenter } from '../documenters/OfficeYamlDocumenter';
export class YamlAction extends BaseAction {
private readonly _officeParameter: CommandLineFlagParameter;
private readonly _newDocfxNamespacesParameter: CommandLineFlagParameter;
private readonly _yamlFormatParameter: CommandLineChoiceParameter<YamlFormat>;
private readonly _yamlFormatParameter: IRequiredCommandLineChoiceParameter<YamlFormat>;

public constructor(parser: ApiDocumenterCommandLine) {
super({
Expand Down
8 changes: 4 additions & 4 deletions apps/trace-import/src/TraceImportCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
CommandLineParser,
type CommandLineFlagParameter,
type CommandLineStringParameter,
type CommandLineChoiceParameter,
type IRequiredCommandLineStringParameter
type IRequiredCommandLineStringParameter,
type IRequiredCommandLineChoiceParameter
} from '@rushstack/ts-command-line';
import { InternalError } from '@rushstack/node-core-library';
import { Colorize } from '@rushstack/terminal';
Expand All @@ -17,7 +17,7 @@ export class TraceImportCommandLineParser extends CommandLineParser {
private readonly _debugParameter: CommandLineFlagParameter;
private readonly _pathParameter: IRequiredCommandLineStringParameter;
private readonly _baseFolderParameter: CommandLineStringParameter;
private readonly _resolutionTypeParameter: CommandLineChoiceParameter<ResolutionType>;
private readonly _resolutionTypeParameter: IRequiredCommandLineChoiceParameter<ResolutionType>;

public constructor() {
super({
Expand Down Expand Up @@ -75,7 +75,7 @@ export class TraceImportCommandLineParser extends CommandLineParser {
traceImport({
importPath: this._pathParameter.value,
baseFolder: this._baseFolderParameter.value,
resolutionType: this._resolutionTypeParameter.value ?? 'cjs'
resolutionType: this._resolutionTypeParameter.value
});
} catch (error) {
if (this._debugParameter.value) {
Expand Down
8 changes: 4 additions & 4 deletions build-tests/ts-command-line-test/src/PushAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
// See LICENSE in the project root for license information.

import {
CommandLineFlagParameter,
type CommandLineFlagParameter,
CommandLineAction,
CommandLineChoiceParameter
type IRequiredCommandLineChoiceParameter
} from '@rushstack/ts-command-line';
import { BusinessLogic } from './BusinessLogic';

type Protocol = 'ftp' | 'webdav' | 'scp';

export class PushAction extends CommandLineAction {
private _force: CommandLineFlagParameter;
private _protocol: CommandLineChoiceParameter<Protocol>;
private _protocol: IRequiredCommandLineChoiceParameter<Protocol>;

public constructor() {
super({
Expand All @@ -24,7 +24,7 @@ export class PushAction extends CommandLineAction {

protected onExecute(): Promise<void> {
// abstract
return BusinessLogic.doTheWork(this._force.value, this._protocol.value || '(none)');
return BusinessLogic.doTheWork(this._force.value, this._protocol.value);
}

protected onDefineParameters(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/api-documenter",
"comment": "",
"type": "none"
}
],
"packageName": "@microsoft/api-documenter"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/trace-import",
"comment": "",
"type": "none"
}
],
"packageName": "@rushstack/trace-import"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/ts-command-line",
"comment": "Update the return type of `defineChoiceParameter`, `defineIntegerParameter`, and `defineStringParameter` respectively when the `defaultValue` option is provided to return `IRequiredCommandLineChoiceParameter`, `IRequiredCommandLineIntegerParameter`, and `IRequiredCommandLineStringParameter` respectively, as the value will definitely be defined in these cases.",
"type": "minor"
}
],
"packageName": "@rushstack/ts-command-line"
}
12 changes: 12 additions & 0 deletions common/reviews/api/ts-command-line.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,28 @@ export abstract class CommandLineParameterProvider {
defineChoiceListParameter<TChoice extends string = string>(definition: ICommandLineChoiceListDefinition<TChoice>): CommandLineChoiceListParameter<TChoice>;
defineChoiceParameter<TChoice extends string = string>(definition: ICommandLineChoiceDefinition<TChoice> & {
required: false | undefined;
defaultValue: undefined;
}): CommandLineChoiceParameter<TChoice>;
defineChoiceParameter<TChoice extends string = string>(definition: ICommandLineChoiceDefinition<TChoice> & {
required: true;
}): IRequiredCommandLineChoiceParameter<TChoice>;
defineChoiceParameter<TChoice extends string = string>(definition: ICommandLineChoiceDefinition<TChoice> & {
defaultValue: TChoice;
}): IRequiredCommandLineChoiceParameter<TChoice>;
defineChoiceParameter<TChoice extends string = string>(definition: ICommandLineChoiceDefinition<TChoice>): CommandLineChoiceParameter<TChoice>;
defineCommandLineRemainder(definition: ICommandLineRemainderDefinition): CommandLineRemainder;
defineFlagParameter(definition: ICommandLineFlagDefinition): CommandLineFlagParameter;
defineIntegerListParameter(definition: ICommandLineIntegerListDefinition): CommandLineIntegerListParameter;
defineIntegerParameter(definition: ICommandLineIntegerDefinition & {
required: false | undefined;
defaultValue: undefined;
}): CommandLineIntegerParameter;
defineIntegerParameter(definition: ICommandLineIntegerDefinition & {
required: true;
}): IRequiredCommandLineIntegerParameter;
defineIntegerParameter(definition: ICommandLineIntegerDefinition & {
defaultValue: number;
}): IRequiredCommandLineIntegerParameter;
defineIntegerParameter(definition: ICommandLineIntegerDefinition): CommandLineIntegerParameter;
// Warning: (ae-forgotten-export) The symbol "CommandLineParameter_2" needs to be exported by the entry point index.d.ts
//
Expand All @@ -190,10 +198,14 @@ export abstract class CommandLineParameterProvider {
defineStringListParameter(definition: ICommandLineStringListDefinition): CommandLineStringListParameter;
defineStringParameter(definition: ICommandLineStringDefinition & {
required: false | undefined;
defaultValue: undefined;
}): CommandLineStringParameter;
defineStringParameter(definition: ICommandLineStringDefinition & {
required: true;
}): IRequiredCommandLineStringParameter;
defineStringParameter(definition: ICommandLineStringDefinition & {
defaultValue: string;
}): IRequiredCommandLineStringParameter;
defineStringParameter(definition: ICommandLineStringDefinition): CommandLineStringParameter;
// @internal
protected abstract _getArgumentParser(): argparse.ArgumentParser;
Expand Down
9 changes: 4 additions & 5 deletions libraries/rush-lib/src/cli/actions/BaseInstallAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import type {
CommandLineFlagParameter,
CommandLineIntegerParameter,
CommandLineStringParameter
CommandLineStringParameter,
IRequiredCommandLineIntegerParameter
} from '@rushstack/ts-command-line';
import { AlreadyReportedError } from '@rushstack/node-core-library';
import { ConsoleTerminalProvider, type ITerminal, Terminal, Colorize } from '@rushstack/terminal';
Expand Down Expand Up @@ -33,7 +34,7 @@ export abstract class BaseInstallAction extends BaseRushAction {
protected readonly _noLinkParameter: CommandLineFlagParameter;
protected readonly _networkConcurrencyParameter: CommandLineIntegerParameter;
protected readonly _debugPackageManagerParameter: CommandLineFlagParameter;
protected readonly _maxInstallAttempts: CommandLineIntegerParameter;
protected readonly _maxInstallAttempts: IRequiredCommandLineIntegerParameter;
protected readonly _ignoreHooksParameter: CommandLineFlagParameter;
protected readonly _offlineParameter: CommandLineFlagParameter;
protected readonly _subspaceParameter: CommandLineStringParameter;
Expand Down Expand Up @@ -223,9 +224,7 @@ export abstract class BaseInstallAction extends BaseRushAction {
}
}

// Because the 'defaultValue' option on the _maxInstallAttempts parameter is set,
// it is safe to assume that the value is not null
if (this._maxInstallAttempts.value! < 1) {
if (this._maxInstallAttempts.value < 1) {
throw new Error(`The value of "${this._maxInstallAttempts.longName}" must be positive and nonzero.`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,23 @@ export abstract class CommandLineParameterProvider {
* ```
*/
public defineChoiceParameter<TChoice extends string = string>(
definition: ICommandLineChoiceDefinition<TChoice> & { required: false | undefined }
definition: ICommandLineChoiceDefinition<TChoice> & {
required: false | undefined;
defaultValue: undefined;
}
): CommandLineChoiceParameter<TChoice>;
/**
* {@inheritdoc CommandLineParameterProvider.(defineChoiceParameter:1)}
*/
public defineChoiceParameter<TChoice extends string = string>(
definition: ICommandLineChoiceDefinition<TChoice> & { required: true }
): IRequiredCommandLineChoiceParameter<TChoice>;
/**
* {@inheritdoc CommandLineParameterProvider.(defineChoiceParameter:1)}
*/
public defineChoiceParameter<TChoice extends string = string>(
definition: ICommandLineChoiceDefinition<TChoice> & { defaultValue: TChoice }
): IRequiredCommandLineChoiceParameter<TChoice>;
/**
* {@inheritdoc CommandLineParameterProvider.(defineChoiceParameter:1)}
*/
Expand Down Expand Up @@ -267,14 +276,20 @@ export abstract class CommandLineParameterProvider {
* ```
*/
public defineIntegerParameter(
definition: ICommandLineIntegerDefinition & { required: false | undefined }
definition: ICommandLineIntegerDefinition & { required: false | undefined; defaultValue: undefined }
): CommandLineIntegerParameter;
/**
* {@inheritdoc CommandLineParameterProvider.(defineIntegerParameter:1)}
*/
public defineIntegerParameter(
definition: ICommandLineIntegerDefinition & { required: true }
): IRequiredCommandLineIntegerParameter;
/**
* {@inheritdoc CommandLineParameterProvider.(defineIntegerParameter:1)}
*/
public defineIntegerParameter(
definition: ICommandLineIntegerDefinition & { defaultValue: number }
): IRequiredCommandLineIntegerParameter;
/**
* {@inheritdoc CommandLineParameterProvider.(defineIntegerParameter:1)}
*/
Expand Down Expand Up @@ -337,14 +352,20 @@ export abstract class CommandLineParameterProvider {
* ```
*/
public defineStringParameter(
definition: ICommandLineStringDefinition & { required: false | undefined }
definition: ICommandLineStringDefinition & { required: false | undefined; defaultValue: undefined }
): CommandLineStringParameter;
/**
* {@inheritdoc CommandLineParameterProvider.(defineStringParameter:1)}
*/
public defineStringParameter(
definition: ICommandLineStringDefinition & { required: true }
): IRequiredCommandLineStringParameter;
/**
* {@inheritdoc CommandLineParameterProvider.(defineStringParameter:1)}
*/
public defineStringParameter(
definition: ICommandLineStringDefinition & { defaultValue: string }
): IRequiredCommandLineStringParameter;
/**
* {@inheritdoc CommandLineParameterProvider.(defineStringParameter:1)}
*/
Expand Down
12 changes: 6 additions & 6 deletions libraries/ts-command-line/src/providers/TabCompletionAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import stringArgv from 'string-argv';

import type { CommandLineIntegerParameter } from '../parameters/CommandLineIntegerParameter';
import type { CommandLineStringParameter } from '../parameters/CommandLineStringParameter';
import type { IRequiredCommandLineIntegerParameter } from '../parameters/CommandLineIntegerParameter';
import type { IRequiredCommandLineStringParameter } from '../parameters/CommandLineStringParameter';
import {
CommandLineParameterKind,
type CommandLineParameterBase,
Expand All @@ -19,8 +19,8 @@ const DEFAULT_WORD_TO_AUTOCOMPLETE: string = '';
const DEFAULT_POSITION: number = 0;

export class TabCompleteAction extends CommandLineAction {
private readonly _wordToCompleteParameter: CommandLineStringParameter;
private readonly _positionParameter: CommandLineIntegerParameter;
private readonly _wordToCompleteParameter: IRequiredCommandLineStringParameter;
private readonly _positionParameter: IRequiredCommandLineIntegerParameter;
private readonly _actions: Map<string, Map<string, CommandLineParameter>>;
private readonly _globalParameters: Map<string, CommandLineParameter>;

Expand Down Expand Up @@ -70,8 +70,8 @@ export class TabCompleteAction extends CommandLineAction {
}

protected async onExecute(): Promise<void> {
const commandLine: string = this._wordToCompleteParameter.value || '';
const caretPosition: number = this._positionParameter.value || (commandLine && commandLine.length) || 0;
const commandLine: string = this._wordToCompleteParameter.value;
const caretPosition: number = this._positionParameter.value || commandLine.length;

for await (const value of this.getCompletions(commandLine, caretPosition)) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 3d704d8

Please sign in to comment.