Skip to content

Commit

Permalink
Fix incompatibilities when upgrading to clap 4.0
Browse files Browse the repository at this point in the history
See the clap CHANGELOG:
https://github.com/clap-rs/clap/blob/v4.0.18/CHANGELOG.md.

`parse(from_os_str)` for a PathBuf just becomes `value_parser`:

  - For `#[clap(parse(from_os_str)]` for `PathBuf`, replace it with
    `#[clap(value_parser)]` (as mentioned earlier this will call
    `value_parser!(PathBuf)` which will auto-select the right `ValueParser`
    automatically).

For `min_values = 2`, this becomes `num_args(2..)`. This is a new
interface that replaces a number of old ones:

    **`Arg::num_args(range)`**

    Clap has had several ways for controlling how many values will be
    captured without always being clear on how they interacted,
    including
    - `Arg::multiple_values(true)`
    - `Arg::number_of_values(4)`
    - `Arg::min_values(2)`
    - `Arg::max_values(20)`
    - `Arg::takes_value(true)`

    These have now all been collapsed into `Arg::num_args` which accepts
    both single values and ranges of values.  `num_args` controls how
    many raw arguments on the command line will be captured as values
    per occurrence and independent of value delimiters.

    See [Issue 2688](clap-rs/clap#2688) for
    more background.

`validator` has been removed, see
https://epage.github.io/blog/2022/06/clap-32-last-call-before-40/. We
were previously using `validator = ...` to set a function to validate
that the value of the DSCP parameter was in the allowed range (0-63).
This no longer requires an entire function and we can just write
`value_parser = clap::value_parser!(u8).range(0..63)`. However, we lose
the ability to detect Windows usage (where we don't support setting the
DSCP value) at argument parsing time.

For setting the verbosity level based on the number of `-v`'s:

  - For `#[clap(parse(from_occurrences))]` replaced with `#[clap(action
    = ArgAction::Count)]` though the field's type must be `u8` (#3794)
  • Loading branch information
edmondsfastly committed Oct 27, 2022
1 parent 0b9f385 commit 3cdfe6b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/bin/dnstap-dump/main.rs
@@ -1,4 +1,4 @@
// Copyright 2021 Fastly, Inc.
// Copyright 2021-2022 Fastly, Inc.

use anyhow::{bail, Result};
use chrono::NaiveDateTime;
Expand Down Expand Up @@ -26,7 +26,7 @@ struct Opts {
#[clap(short = 'r',
long = "read",
name = "FILE",
parse(from_os_str),
value_parser,
value_hint = ValueHint::FilePath)
]
file: PathBuf,
Expand Down
31 changes: 9 additions & 22 deletions src/bin/dnstap-replay/main.rs
Expand Up @@ -2,7 +2,7 @@

use anyhow::Result;
use async_channel::{bounded, Receiver, Sender};
use clap::{Parser, ValueHint};
use clap::{ArgAction, Parser, ValueHint};
use log::*;
use std::net::SocketAddr;
use std::path::PathBuf;
Expand Down Expand Up @@ -88,7 +88,9 @@ pub struct Opts {
dns: SocketAddr,

/// DSCP value to set on outgoing queries
#[clap(long, name = "DSCP code point", validator = is_dscp)]
#[clap(long,
name = "DSCP code point",
value_parser = clap::value_parser!(u8).range(0..63))]
dscp: Option<u8>,

/// HTTP server socket to listen on for stats and reporting
Expand All @@ -115,8 +117,8 @@ pub struct Opts {
#[clap(long = "match-status-files",
name = "STATUS-FILE",
required = false,
min_values = 2,
parse(from_os_str),
num_args(2..),
value_parser,
value_hint = ValueHint::FilePath)
]
status_files: Vec<PathBuf>,
Expand All @@ -126,23 +128,8 @@ pub struct Opts {
unix: String,

/// Increase verbosity level
#[clap(short, long, parse(from_occurrences))]
verbose: usize,
}

#[cfg(unix)]
fn is_dscp(val: &str) -> Result<(), String> {
// Parse 'val' as an integer, but only allow values between 0 and 63 inclusive since the DSCP
// field is a 6-bit quantity.
match val.parse() {
Ok(0..=63) => Ok(()),
_ => Err(String::from("DSCP code point must be in the range [0..63]")),
}
}

#[cfg(not(unix))]
fn is_dscp(_val: &str) -> Result<(), String> {
Err(String::from("Cannot set DSCP values on this platform"))
#[clap(short, long, action = ArgAction::Count)]
verbose: u8,
}

impl Server {
Expand Down Expand Up @@ -259,7 +246,7 @@ fn main() -> Result<()> {
let opts = Opts::parse();

stderrlog::new()
.verbosity(opts.verbose)
.verbosity(opts.verbose as usize)
.module(module_path!())
.init()
.unwrap();
Expand Down

0 comments on commit 3cdfe6b

Please sign in to comment.