Open KeithBird opened 2 years ago
👍👍👍 When will the feature be released?
On the topic of future work, one thing I've done in other tools that's enabled by this is to automatically prefix-match options/subcommands when there is no ambiguity. This obviously requires interactive mode detection to ensure shell scripts aren't broken by new options/subcommands being added in the future.
This is great, any plan to merge this feature/interactive
branch?
Introduction
ArgumentParser provides a straightforward way to declare command-line interfaces in Swift, with the dual goals of making it (1) fast and easy to create (2) high-quality, user-friendly CLI tools.
In order to further achieve these two goals, for this project, we designed and implemented an interactive mode for tools built using ArgumentParser. This mode can prompt for required arguments not given in the initial command, suggest possible corrections when user input is invalid, help users learn to use unfamiliar command line tools by trial and error.
This project was also done as a GSoC (Google Summer of Code) project for which you can find here.
Motivation
With server side swift gaining more and more traction, command line apps built with ArgumentParser can be very useful for automating common tasks to boost developer productivity. But there are still many developers don’t want to bother with the command line, because the help text came in the form of thick manuals and error messages were opaque.
For example, in the past, users would get lengthy error messages when required arguments are not initialized:
Introducing the conversational nature of interactive mode will be very helpful, it can reduce duplication and provide a conversational CLI which is both easier to write and easier to read:
Achievements
Ask
prompt the user for inputThe above code will generate the following dialog:
Check
verifies key directivesThe above code will generate the following dialog:
Choose
provides possible input for the user to choose fromThe above code will generate the following dialog:
Ask for required
@Argument
When parameters similar to the following are not initialized:
The following dialog will be generated automatically:
Ask for required
@Option
When parameters similar to the following are not initialized:
The following dialog will be generated automatically:
Ask for required
@Flag: EnumerableFlag
When parameters similar to the following are not initialized:
The following dialog will be generated automatically:
Asking to retype an invalid value
When parameters similar to the following are not initialized:
The following dialog will be generated automatically:
The above is just a partial display of the interfaces. For more details, please click the links in the implementation section.
Implementation
This project is implemented by the following subtasks, you can click to see more detailed API design:
#448
#450
#453
#467
#469
#459
#462
Future Work
Fixing misspelled arguments
If a user mistypes an option, flag, or command, the interactive mode should suggest possible correction:
Merge two
canInteract()
functionsSince the two
canInteract()
one modifies theSplitArguments
and the other modifies theParsedValues
, it's not a good idea to merge them for now. But it could make more sense if there's only one path for interactively collecting additional input. So we need find a way to handle.missingValueForOption
error without modifying the the original input, which is surely going to be more error prone than operating on the more structured parsed value data.The discussion under the #451 provides more details.
Related Links
GSoC accepted proposal
GSoC official project showcase page
Swift forum posts related to this project
Other GSoC projects organizing in swift