From 9b36246ed3808bb2d10618ad5eb3344a2009a227 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 8 Jan 2024 10:28:24 -0500 Subject: [PATCH] examples: replace structopt with clap See https://github.com/TeXitoi/structopt/issues/525. Currently codespan-reporting is the second most downloaded dependent of structopt: https://crates.io/crates/structopt/reverse_dependencies. --- codespan-reporting/Cargo.toml | 4 +++- codespan-reporting/examples/readme_preview.rs | 14 +++++------- .../examples/reusable_diagnostic.rs | 14 +++++------- codespan-reporting/examples/term.rs | 14 +++++------- codespan-reporting/src/term.rs | 22 +++++++++---------- 5 files changed, 31 insertions(+), 37 deletions(-) diff --git a/codespan-reporting/Cargo.toml b/codespan-reporting/Cargo.toml index 91b91c7c5..428f2f87f 100644 --- a/codespan-reporting/Cargo.toml +++ b/codespan-reporting/Cargo.toml @@ -22,7 +22,9 @@ insta = "1.6.3" lazy_static = "1.4" peg = "0.7" rustyline = "6" -structopt = "0.3" +# Latest is 4.4.13 but specifies MSRV in Cargo.toml which means we can't depend +# on it (even though we won't compile it in MSRV CI). +clap = { version = "3.2.25", features = ["derive"] } unindent = "0.1" [features] diff --git a/codespan-reporting/examples/readme_preview.rs b/codespan-reporting/examples/readme_preview.rs index f5a6468a8..e45da022b 100644 --- a/codespan-reporting/examples/readme_preview.rs +++ b/codespan-reporting/examples/readme_preview.rs @@ -7,27 +7,25 @@ //! cargo run --example readme_preview svg > codespan-reporting/assets/readme_preview.svg //! ``` +use clap::Parser; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFile; use codespan_reporting::term::termcolor::{Color, ColorSpec, StandardStream, WriteColor}; use codespan_reporting::term::{self, ColorArg}; use std::io::{self, Write}; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "emit")] +#[derive(Debug, Parser)] +#[clap(name = "emit")] pub enum Opts { /// Render SVG output Svg, /// Render Stderr output Stderr { /// Configure coloring of output - #[structopt( + #[clap( long = "color", - parse(try_from_str), default_value = "auto", - possible_values = ColorArg::VARIANTS, - case_insensitive = true + value_parser = clap::builder::PossibleValuesParser::new(ColorArg::VARIANTS), )] color: ColorArg, }, @@ -77,7 +75,7 @@ fn main() -> anyhow::Result<()> { )])]; // let mut files = SimpleFiles::new(); - match Opts::from_args() { + match Opts::parse() { Opts::Svg => { let mut buffer = Vec::new(); let mut writer = HtmlEscapeWriter::new(SvgWriter::new(&mut buffer)); diff --git a/codespan-reporting/examples/reusable_diagnostic.rs b/codespan-reporting/examples/reusable_diagnostic.rs index d05dee854..54971823f 100644 --- a/codespan-reporting/examples/reusable_diagnostic.rs +++ b/codespan-reporting/examples/reusable_diagnostic.rs @@ -1,18 +1,16 @@ +use clap::Parser; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFile; use codespan_reporting::term::termcolor::StandardStream; use codespan_reporting::term::{self, ColorArg}; use std::ops::Range; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "emit")] +#[derive(Debug, Parser)] +#[clap(name = "emit")] pub struct Opts { - #[structopt(long = "color", - parse(try_from_str), + #[clap(long = "color", default_value = "auto", - possible_values = ColorArg::VARIANTS, - case_insensitive = true + value_parser = clap::builder::PossibleValuesParser::new(ColorArg::VARIANTS), )] color: ColorArg, } @@ -38,7 +36,7 @@ fn main() -> anyhow::Result<()> { Error::MutatingImmutable(Item::new(20..23, "foo"), Item::new(51..59, "foo += 1")), ]; - let opts = Opts::from_args(); + let opts = Opts::parse(); let writer = StandardStream::stderr(opts.color.into()); let config = codespan_reporting::term::Config::default(); for diagnostic in errors.iter().map(Error::report) { diff --git a/codespan-reporting/examples/term.rs b/codespan-reporting/examples/term.rs index ee2c057d7..8fbffec87 100644 --- a/codespan-reporting/examples/term.rs +++ b/codespan-reporting/examples/term.rs @@ -5,28 +5,26 @@ //! cargo run --example term //! ``` +use clap::Parser; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFiles; use codespan_reporting::term::termcolor::StandardStream; use codespan_reporting::term::{self, ColorArg}; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "emit")] +#[derive(Debug, Parser)] +#[clap(name = "emit")] pub struct Opts { /// Configure coloring of output - #[structopt( + #[clap( long = "color", - parse(try_from_str), default_value = "auto", - possible_values = ColorArg::VARIANTS, - case_insensitive = true + value_parser = clap::builder::PossibleValuesParser::new(ColorArg::VARIANTS), )] pub color: ColorArg, } fn main() -> anyhow::Result<()> { - let opts = Opts::from_args(); + let opts = Opts::parse(); let mut files = SimpleFiles::new(); let file_id1 = files.add( diff --git a/codespan-reporting/src/term.rs b/codespan-reporting/src/term.rs index 0e75a1424..31d3e15e3 100644 --- a/codespan-reporting/src/term.rs +++ b/codespan-reporting/src/term.rs @@ -16,32 +16,31 @@ pub use self::config::{Chars, Config, DisplayStyle, Styles}; /// A command line argument that configures the coloring of the output. /// -/// This can be used with command line argument parsers like [`clap`] or [`structopt`]. +/// This can be used with command line argument parsers like [`clap`] or [`Parser`]. /// /// [`clap`]: https://crates.io/crates/clap -/// [`structopt`]: https://crates.io/crates/structopt +/// [`Parser`]: https://crates.io/crates/Parser /// /// # Example /// /// ```rust /// use codespan_reporting::term::termcolor::StandardStream; /// use codespan_reporting::term::ColorArg; -/// use structopt::StructOpt; +/// use clap::Parser; /// -/// #[derive(Debug, StructOpt)] -/// #[structopt(name = "groovey-app")] +/// #[derive(Debug, Parser)] +/// #[clap(name = "groovey-app")] /// pub struct Opts { /// /// Configure coloring of output -/// #[structopt( +/// #[clap( /// long = "color", /// default_value = "auto", -/// possible_values = ColorArg::VARIANTS, -/// case_insensitive = true, +/// value_parser = clap::builder::PossibleValuesParser::new(ColorArg::VARIANTS), /// )] /// pub color: ColorArg, /// } /// -/// let opts = Opts::from_args(); +/// let opts = Opts::parse(); /// let writer = StandardStream::stderr(opts.color.into()); /// ``` #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -50,11 +49,10 @@ pub struct ColorArg(pub ColorChoice); impl ColorArg { /// Allowed values the argument. /// - /// This is useful for generating documentation via [`clap`] or `structopt`'s - /// `possible_values` configuration. + /// This is useful for generating documentation via [`clap`]'s + /// `value_parser = clap::builder::PossibleValuesParser::new` configuration. /// /// [`clap`]: https://crates.io/crates/clap - /// [`structopt`]: https://crates.io/crates/structopt pub const VARIANTS: &'static [&'static str] = &["auto", "always", "ansi", "never"]; }