Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ts-command-line] Add defineCommandLineRemainder() feature #1869

Merged
merged 21 commits into from
May 15, 2020

Conversation

octogonz
Copy link
Collaborator

This PR adds a new feature to ts-command-line that enables the remaining CLI arguments to be captured, without any validation. We want this for #1232 (comment) so that rushx can pass its arguments along to another command.

While I was in there, I fixed a bunch of other minor issues with ts-command-line:

  • Fix a bug with environmentVariable mapping for CommandLineFlagParameter
  • Add the ability for an environment variable to specify multiple values for CommandLineStringListParameter, encoded as a JSON array
  • Use API Extractor to trim internal APIs from the .d.ts rollup
  • Fix some bugs that prevented a CommandLineParser from being defined without any actions
  • Improve the README.md and API documentation

I also added a ts-command-line-test project that makes it easier to debug the library.

// with escaping here seems unwise, since there are so many shell escaping mechanisms that could
// potentially confuse the experience.
this._values = [ environmentValue ];
if (environmentValue[0] === '[') {
Copy link
Member

Choose a reason for hiding this comment

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

Can environmentVariableValue be an empty string ""?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In Bash, it is easy to assign an empty string to an environment variable:

export MY_VARIABLE=

For the DOS cmd.exe shell, it is possible for the value to be an empty string, but you cannot use SET to assign it -- doing so will delete the variable. So it has to be assigned using some other tool. PowerShell can probably do it.

If you're asking about the design here, because an empty string does not start with [, it will simply become the first item in the list.

Copy link
Member

Choose a reason for hiding this comment

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

I was asking because if environmentValue can be "" then accessing environmentValue[0] can throw whenever the string is empty but not undefined. The string can become "" if it was originally whitespace entirely and then we trimmed the string.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes in C#, not true in JavaScript.

// with escaping here seems unwise, since there are so many shell escaping mechanisms that could
// potentially confuse the experience.
this._values = [ environmentValue ];
if (environmentValue[0] === '[') {
Copy link
Member

Choose a reason for hiding this comment

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

Can environmentVariableValue start with whitespace characters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, but then it will not be parsed as an array. The entire string will simply become the first item in the list.

Hmm... that might be counterintuitive if a person is typing the value into a text box in a web page (e.g. AzureDev Ops). I'll trim it. 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I also documented the parsing rules.

@@ -0,0 +1,20 @@
const fsx = require('fs-extra');
Copy link
Member

Choose a reason for hiding this comment

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

Why not FileSystem?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This library predates node-core-library. If we ever do an operation that would genuinely benefit, I'll add it as a dependency.

},
"include": [
"src/**/*.ts",
"typings/tsd.d.ts"
Copy link
Member

Choose a reason for hiding this comment

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

Remove?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This project doesn't use RSC because it's intended as a code sample.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh right, but tsd.d.ts isn't needed; removed.

Comment on lines +265 to +268
if (!this.selectedAction) {
return Promise.resolve();
}
return this.selectedAction._execute();
Copy link
Member

Choose a reason for hiding this comment

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

Why not turn this into an async function and:

Suggested change
if (!this.selectedAction) {
return Promise.resolve();
}
return this.selectedAction._execute();
if (this.selectedAction) {
await this.selectedAction._execute();
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm going to bulk-convert everything to async/await, but not in this PR. It needs to be a standalone PR.


return commandLineParser.execute(['--flag', 'the', 'remaining', 'args']).then(() => {
expect(commandLineParser.selectedAction).toBeUndefined();
// expect(commandLineParser.flag.value).toBe(true);
Copy link
Member

Choose a reason for hiding this comment

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

Remove?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, that was not meant to be disabled. I've uncommented it.

@octogonz octogonz force-pushed the octogonz/tsc-improvements branch from 4d07147 to bcc3be1 Compare May 15, 2020 07:44
@octogonz octogonz merged commit 818e085 into master May 15, 2020
@octogonz octogonz deleted the octogonz/tsc-improvements branch May 15, 2020 07:56
@octogonz
Copy link
Collaborator Author

This was published as ts-command-line version 4.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants