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

Replace positional arguments with flags in CLI #2275

Merged
merged 39 commits into from
Jun 28, 2022

Conversation

ljoss17
Copy link
Contributor

@ljoss17 ljoss17 commented Jun 7, 2022

Closes: #2239

Description

This PR adds flags instead of positional arguments to all CLI commands. Long flags have been used in order to avoid conflicts or confusing flags.

WIP

  • At commit 51852ae: Some short flags are still present for some commands, but a discussion in order to clarify if all the short flags will be removed or if some strategic short flags will be kept is ongoing.
  • At commit 88474db and a76599f: Removed all short flags.

PR author checklist:

  • Added changelog entry, using unclog.
  • Added tests: integration (for Hermes) or unit/mock tests (for modules).
  • Linked to GitHub issue.
  • Updated guide (e.g., guide/).

Reviewer checklist:

  • Reviewed Files changed in the GitHub PR explorer.
  • Manually tested (in case integration/unit/mock tests are absent).

@plafer
Copy link
Contributor

plafer commented Jun 8, 2022

Just a note not to forget to update the gm hermes cc command. This is the current output:

% gm hermes cc
"./hermes" create channel ibc0 ibc1 --port-a transfer --port-b transfer

@ljoss17
Copy link
Contributor Author

ljoss17 commented Jun 9, 2022

Just a note not to forget to update the gm hermes cc command. This is the current output:

% gm hermes cc
"./hermes" create channel ibc0 ibc1 --port-a transfer --port-b transfer

Yes, I will check all the commands which use hermes and update them with the new flags.

@ljoss17 ljoss17 marked this pull request as ready for review June 10, 2022 13:46
@hu55a1n1
Copy link
Member

Just some pointers to help add unit-tests for clap CLI arg parsing -> clap-rs/clap#1355 (comment)

@ljoss17 ljoss17 marked this pull request as draft June 15, 2022 14:57
fn test_keys_add_key_and_mnemonic() {
assert!(KeysAddCmd::try_parse_from(&["test", "--chain", "chain_id", "--key-file", "key_file", "--mnemonic-file", "mnemonic_file"]).is_err());
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @hu55a1n1 @mzabaluev do these examples for unit-testing the clap arg parsing seem good to you ? Or do you have any feedback regarding unit-testing the clap arg parsing ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is great! Given how many changes are/ will be in this PR already, can we do the clap tests in a different PR?

Copy link
Member

Choose a reason for hiding this comment

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

This is exactly what I had in mind! 👌 Would be nice to get this in before relayer-cli v1. I agree it's better to do it in a different PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was hoping there is some framework to provide some handy test macros, but I can't find any at the moment.
This will do, we can add more cases to exercise.

If you have time, consider creating some helper macros like:

assert_valid_cmdline!("--chain", "chain_id", "--key-file", "key_file", "--mnemonic-file", "mnemonic_file");
assert_invalid_cmdline!("--chain", "chain_id", "--key-file", "key_file", "--mnemonic-file", "mnemonic_file");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for all the feedback! I will check this as soon as this PR is merged and I work on the unit-tests.

@mzabaluev
Copy link
Contributor

I have found trycmd, which looks like a useful tool to organize CLI tests in a self-documented way.

This could also augment our manually maintained command examples in the guide mdbook. Not in scope for this PR, though.

@ljoss17 ljoss17 marked this pull request as ready for review June 20, 2022 07:25
@romac
Copy link
Member

romac commented Jun 24, 2022

Great stuff! Just one question: what's the rationale behind using --chan instead of --channel?

@ljoss17
Copy link
Contributor Author

ljoss17 commented Jun 24, 2022

Since we use only long flags, the idea was to reduce the flag length for cases which are still meaningful after being shortened. This was done for the flags --connection, --channel and --sequence, that became --conn, --chan and --seq.

@romac
Copy link
Member

romac commented Jun 24, 2022

Is there an easy way to allow both --chan and --channel? (respectively conn/connection, seq/sequence)

@ljoss17
Copy link
Contributor Author

ljoss17 commented Jun 24, 2022

Is there an easy way to allow both --chan and --channel? (respectively conn/connection, seq/sequence)

Yes, with clap's alias. For example if we add the alias to the the channel_id in the QueryChannelEndCmd:

    #[clap(
        long = "chan",
        alias = "channel",
        required = true,
        value_name = "CHANNEL_ID",
        help = "identifier of the channel to query"
    )]
    channel_id: ChannelId,

Then both these tests will pass:

    #[test]
    fn test_query_channel_end_required_only() {
        assert_eq!(
            QueryChannelEndCmd{ chain_id: ChainId::from_string("chain_id"), port_id: PortId::from_str("port_id").unwrap(), channel_id: ChannelId::from_str("channel-07").unwrap(), height: None },
            QueryChannelEndCmd::parse_from(&["test", "--chain", "chain_id", "--port", "port_id", "--chan", "channel-07"])
        )
    }

    #[test]
    fn test_query_channel_end_required_only_alias() {
        assert_eq!(
            QueryChannelEndCmd{ chain_id: ChainId::from_string("chain_id"), port_id: PortId::from_str("port_id").unwrap(), channel_id: ChannelId::from_str("channel-07").unwrap(), height: None },
            QueryChannelEndCmd::parse_from(&["test", "--chain", "chain_id", "--port", "port_id", "--channel", "channel-07"])
        )
    }

@romac
Copy link
Member

romac commented Jun 24, 2022

Sweet, then let's do this. Remains to decide whether we want the long (channel) or shortened (chan) version to be the default one? What do you think? I would vote for the long one being the default and the shorter one as alias, but would like to hear what you and others think?

@ljoss17
Copy link
Contributor Author

ljoss17 commented Jun 24, 2022

I have a preference for the shortened version, but I think my opinion is biased due to using them when replacing and testing the commands.
Should the aliases be added in the PR adding the parsing unit-tests, which I will work on as soon as this PR is merged ? Or should they be added in a separate PR which would only add the aliases ?

@seanchen1991
Copy link
Contributor

I would also agree with sticking with the long versions, if only to be more explicit. I think this applies to conn and connection as well.

@romac
Copy link
Member

romac commented Jun 27, 2022

Should the aliases be added in the PR adding the parsing unit-tests, which I will work on as soon as this PR is merged ? Or should they be added in a separate PR which would only add the aliases ?

I would rather add the aliases to this PR to keep PR churn down, if that's okay.

Copy link
Contributor

@mzabaluev mzabaluev left a comment

Choose a reason for hiding this comment

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

Good work!

I've reviewed "my turf"...

health-check, help

These were easy: no parameters, nothing to change!
So I took it upon myself to look at some more commands.

guide/src/commands/relaying/clear.md Show resolved Hide resolved
guide/src/commands/path-setup/index.md Outdated Show resolved Hide resolved
relayer-cli/src/commands/create/channel.rs Outdated Show resolved Hide resolved
relayer-cli/src/commands/create/channel.rs Outdated Show resolved Hide resolved
relayer-cli/src/commands/create/connection.rs Show resolved Hide resolved
relayer-cli/src/commands/listen.rs Outdated Show resolved Hide resolved
@mzabaluev
Copy link
Contributor

Please also merge with recent master as there are some new conflicts.

@romac
Copy link
Member

romac commented Jun 28, 2022

What's the reasoning behind having --a-chain instead of --chain-a? I am asking because the latter reads better to me.

@ljoss17
Copy link
Contributor Author

ljoss17 commented Jun 28, 2022

What's the reasoning behind having --a-chain instead of --chain-a? I am asking because the latter reads better to me.

The choice of prefixing --a- and --b- is due to the following discussion in the ADR PR : #2306 (comment)

Copy link
Member

@romac romac left a comment

Choose a reason for hiding this comment

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

Amazing work @ljoss17, thank you so much! Many thanks to all reviewers as well :)

@adizere adizere merged commit 57b8af9 into master Jun 28, 2022
@adizere adizere deleted the luca_joss/replace_positional_args_with_flags branch June 28, 2022 13:08
mmsqe added a commit to mmsqe/pystarport that referenced this pull request Jul 27, 2022
yihuang pushed a commit to crypto-com/pystarport that referenced this pull request Jul 28, 2022
hu55a1n1 pushed a commit to hu55a1n1/hermes that referenced this pull request Sep 13, 2022
* Updated CLI commands to take flags everywhere and updated e2e tests accordingly

* Updated gm to use flags when calling Hermes

* Added missing flags to e2e test for 'query client state' command

* Fixed flag errors in e2e tests and removed conflicting short flag.

* Removed all short flags and updated CLI commands comments

* Removed forgotten short flags.

* Updated Hermes guide with flags instead of positional arguments

* Updated script and comment with new long flags for Hermes

* Completed 'tx raw upgrade-' commands guide page. Updated Testing client upgrade guide page

* Added changelog entry

* Added example unit-tests to the 'keys add' command

* Added value names to parameters and removed cli parsing unit-tests

* Cargo fmt changes

* Updated flags in order to reflect ADR 010

* Updated guide to reflect flag changes from ADR 010

* Updated gm script and e2e tests to match flag changes from ADR 010

* Fixed ADR 010 typo

* Remove short flags from `key` error messages

* Fix inconsistent error messaging in channel.rs

* Fix more inconsistent error messaging

* Run cargo fmt

* Fix commands that use deprecated `-c` flag

* Added the query transfer subcommand in docs.

* Scrub query clients

* Scrub query client state

* Removed excessive debug line

* Disabled filtering for query channels CLI

* Added aliases for 'connection', 'channel' and 'sequence' flags

* Fixed error in CLI replacing HeightQuery with QueryHeight

* Fix cargo fmt

* Changed 'version' to 'channel-version' for 'hermes create channel' command

* Separated 'hermes create channel' help into short and long message

* Updated 'hermes listen' so that '--events' flag can take multiple values

* Updated guide with new '--events' flag for 'hermes listen'

* Start all arguments help text with an uppercase letter, to match Clap's help for autogenerated arguments (eg. --help)

* Update guide to account for argument help text starting with an uppercase letter

* Add shortened aliases for `tx raw` commands

Co-authored-by: Sean Chen <seanchen11235@gmail.com>
Co-authored-by: Adi Seredinschi <adi@informal.systems>
Co-authored-by: Romain Ruetschi <romain@informal.systems>
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.

CLI: Use flags everywhere instead of positional arguments
8 participants