shadowsocks/shadowsocks-rust · error

`method` is required

Error message

`method` is required

What it means

get_one::<String>("ENCRYPT_METHOD") returns None when the required encrypt-method argument was not supplied, and .expect("`method` is required") panics with this message. Normally clap's required-argument validation catches this first; the expect is a defensive backstop.

Source

Thrown at src/service/genkey.rs:31

        Arg::new("ENCRYPT_METHOD")
            .short('m')
            .long("encrypt-method")
            .num_args(1)
            .action(ArgAction::Set)
            .required(true)
            .value_parser(PossibleValuesParser::new(available_ciphers()))
            .help("Server's encryption method"),
    );

    app
}

/// Program entrance `main`
pub fn main(matches: &ArgMatches) -> ExitCode {
    let method = matches
        .get_one::<String>("ENCRYPT_METHOD")
        .map(|x| x.parse::<CipherKind>().expect("method"))
        .expect("`method` is required");

    let key_len = method.key_len();
    if key_len > 0 {
        let mut key = vec![0u8; key_len];
        rand::fill(key.as_mut_slice());

        let encoded_key = base64::engine::general_purpose::STANDARD.encode(&key);
        println!("{encoded_key}");
    }

    ExitCode::SUCCESS
}

View on GitHub (pinned to 8eb0f0a65b)

Solutions

  1. Pass the encrypt-method argument on the command line, e.g. `genkey aes-256-gcm`.
  2. If calling genkey::main programmatically, insert "ENCRYPT_METHOD" into the ArgMatches.
  3. Ensure clap's arg definition marks ENCRYPT_METHOD as required so clap reports it cleanly.

Example fix

// before
let matches = cmd.try_get_matches_from(["genkey"])?;
genkey::main(&matches);

// after
let matches = cmd.try_get_matches_from(["genkey", "aes-256-gcm"])?;
genkey::main(&matches);
Defensive patterns

Strategy: validation

Validate before calling

// before calling genkey entry point programmatically
if matches.get_one::<String>("ENCRYPT_METHOD").is_none() {
    return Err("genkey requires an encrypt-method argument".into());
}

Try / catch

let method = matches.get_one::<String>("ENCRYPT_METHOD")
    .ok_or_else(|| anyhow!("`method` is required"))?;

Prevention

When it happens

Trigger: Invoking the genkey subcommand without the ENCRYPT_METHOD positional/option, bypassing clap validation (e.g. calling service::genkey::main programmatically with hand-built ArgMatches).

Common situations: Programmatic calls into the service main with hand-constructed matches; script wrappers dropping an argument; clap config regressions that made the arg optional.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of shadowsocks/shadowsocks-rust@8eb0f0a65b (2026-09-09). Data as JSON: /api/errors/d4bf1f7493a55236. Report an issue: GitHub.