Migrate CLI to `clap`
This issue is meant to track the migration progress and give some implementation guidance.
Status (31/31)
-
authinpatch/59997806eead5e3b8adf117981493b434f940431 -
blockinpatch/224efaaca8b13208f3d3beafcc37b2d229b13901 -
checkoutinpatch/b6495653409be30de353a445602e2e5a0ac604d5 -
cleaninpatch/2fa2dcf7b64e7d60a39fff050e9e4057ef41ea54 -
cloneinpatch/c0bcf014517eee36c3693bd62601670410cfc6d2 -
cobinpatch/490dedd8f7520f12e1d0aba917e4103774e67605 -
configinpatch/dcbcb7e8bcc30d3f6ea97b3d338ede8bd96cbf81 -
debuginpatch/343073d3a5999d3c81a308bfeb5d7b727ec14e8b -
diffinpatch/61f78b2ff73747b7f77b5cd9517ed1db183d4ff5 -
followinpatch/947e68e86c577ac348e7292b6e0f07ce52f1ac2f -
forkinpatch/e5416429d8e19b065b85ad9a5e3afcd8265f162f -
idinpatch/9e45558dda40cf7e46739fef11b366d2060ecb1d -
inbox(claimed) -
initinpatch/6719913fa5e596f5946e2a59141f31c4a5b51fcb -
inspectinpatch/3097a313c10fb5800026755f0634634582f14aea -
issueinpatch/976756d8bf1f4e157ec8c1dc6818f670f8b4d726 -
lsinpatch/8d79097f8ffa950245b46b3930da07e651a58821 -
nodeinpatch/77453b5ea69bc07aed212f9e0c91332c16295ea1 -
patchinpatch/f9f6791c0b080cfe449e2e0cc4d514c1f82f9e59 -
pathinpatch/75520da0d6f3f2f8611ba8d944ae61047e66b19c -
publishinpatch/f3496b1634707f7f42a0245f7ed1ed59b4de828e -
remoteinpatch/e9defa94038506c9187930ba82be10c4a888acc6 -
seedinpatch/2b9404287827fd5e4d0c9702cd6df8ebfda0758b -
selfinpatch/908c750d6434403903da44c5ec64d81f2bddf119 -
statsinpatch/5311080da9c756cea39d0c55b818f2243da8d0f2 -
syncinpatch/bc69c2c2c0b942166e7286576b50e05f80763076 -
unblockinpatch/9a9b58fa02db817cc4597c815908eacf5b936b6d -
unfollowinpatch/01fde0e26aa0c735f89f78ed50efb1a284360038 -
unseedinpatch/06b4d4851c54d43d23cf38d8661b52b1526c353f -
watchinpatch/b4b7e7eabad05d1b1272c6b2ba43d5c37e46f9fe -
raditself
Migrating a command
- Command behaviour should not change. Do not add flags or change the semantics.
- CLI tests should not change. Adding new tests that cover the current behaviour is appreciated though.
- First, migrate arguments to
claponly, touching as little code as possible. Do refactorings in small, subsequent commits only after. - Make sure help and error coloring follows
rad issue. - Using one-liners for argument description is preferred.
- Attribute macro usage on commands and args should follow
rad issue - Argument values should be named consistently across commands, e.g.
ISSUE_ID,PATCH_ID,REVISION_ID, etc.
A Step-by-Step Recommendation
Create ABOUT and LONG_ABOUT
- Change
pub const HELP: Help = ...topub(crate) const ABOUT: &str = "<Help::description>";andLONG_ABOUT: &str = "<Help::usage>", editing the usage to fitclaps “long about”. - Remove
LONG_ABOUTif it is empty or trivial.
Migrate from struct Options to struct Args
- Remove
use terminal::args::Args. - Change
impl Args for Optionsto justimpl Options. - Use LSP to rename
OptionstoArgs. - Annotate
Argswith#[derive(Debug, Parser)], addinguse clap::Parser - Annotate
Argswith#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)] - Do the actual migration by annotating members of
Args. This is the part to really think about. Look atfn from_argsclosely. - Remove the whole
impl Argsblock.
Create args module
- Create a new file
<command>/args.rs. - Move
struct Argsand all associated types, as well asconst ABOUTandconst LONG_ABOUT(if it exists) to the new moduleargs. Fix imports. - Wire up new module in
<command>.rs: Addmod args,pub use args::Argsandpub(crate) use args::ABOUT. - Add unit tests if the command uses custom parsing logic via e.g.
value_parserfor types likeRepoIDorNodeId.
Wire up main.rs
- Add a new variant for your command to
enum Commands. - Search for “run_command_fn”, copy the match arm.
- Search for the command you are migrating, replace its match arm.
Wire up help.rs
- Search for “CommandItem::Clap”, copy the constructor.
- Search for the command you are migrating, replace the
CommandItem::Lexoptconstructor with theCommandItem::Clapconstructor you copied, and edit it to fit.
Also a note: examples that are used for testing errors will need the
(stderr)annotation… and probably also a modification of the error message, as clap error output is a bit more wordy.auth: patch 59997806eead5e3b8adf117981493b434f940431checkout: patch b6495653409be30de353a445602e2e5a0ac604d5init: patch 6719913fa5e596f5946e2a59141f31c4a5b51fcbGotta admit that the patch looks a little weird, ’cause
--privateand--publicare semantically the same flag, just with different values, with--publicbeing default. I just haven’t found a sane way to handle this with Clap…ls: patch 8d79097f8ffa950245b46b3930da07e651a58821inspect: patch 3097a313c10fb5800026755f0634634582f14aeaNow ready!
If
syncis still available tomorrow, I’ll dive into it as well. UPDATE: someone claimed it (not me), so won’tinbox: patch 35a73191e5c4d8d09ba0192a81a8b9f09fd5028e