1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[clap-rs/clap]勝手に追加されるhelpサブコマンドを消す

Last updated at Posted at 2022-04-03

概要

サブコマンドを有効化すると出てくる help の消し方がわからなかったので調べた。

$ cli --help
cli 1.0

USAGE:
    cli <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    help     Print this message or the help of the given subcommand(s) ←こいつ!!
    sub-a    サブコマンドA
main.rs
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[clap(version = "1.0")]
struct Args {
    #[clap(subcommand)]
    action: Action,
}

#[derive(Subcommand, Debug)]
enum Action {
    /// サブコマンドA
    SubA
}

fn main() {
    Args::parse();
}

消し方

メソッドチェーンで定義してる場合は↓の指定を追加してやれば消えてくれる1らしい。 (未検証)

rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

Deriveして使ってる場合

これが知りたかったのに直接それっぽい情報が見つからなかった..
clap の Derive Reference を調べてみると、引数でいろいろ指定できるらしいことが判明。

Raw attributes: Any Command method can also be used as an attribute, see Terminology for syntax.

e.g. #[clap(arg_required_else_help(true))] would translate to cmd.arg_required_else_help(true)

ざっと探してそれっぽいものを発見。
disable_help_subcommand(self, yes: bool)

追加して実行してみる。

main.rs
#[derive(Parser, Debug)]
- #[clap(version = "1.0")]
+ #[clap(version = "1.0", disable_help_subcommand(true))]
struct Args {
$ cli --help
cli 1.0

USAGE:
    cli <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    sub-a    サブコマンドA

消えてくれた! めでたし🎉

まとめ

Deriveだと細かい設定出来ないのかと思ってたけど、そんなことなさそうで良かった。

  1. https://github.com/spf13/cobra/issues/587#issuecomment-810159087

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?