Skip to content

Commit

Permalink
examples: replace structopt with clap
Browse files Browse the repository at this point in the history
See TeXitoi/structopt#525.

Currently codespan-reporting is the second most downloaded dependent of
structopt: https://crates.io/crates/structopt/reverse_dependencies.
  • Loading branch information
tamird committed Jan 8, 2024
1 parent c84116f commit e4e65da
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion codespan-reporting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ insta = "1.6.3"
lazy_static = "1.4"
peg = "0.7"
rustyline = "6"
structopt = "0.3"
clap = { version = "4.4.13", features = ["derive"] }
unindent = "0.1"

[features]
Expand Down
14 changes: 6 additions & 8 deletions codespan-reporting/examples/readme_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ 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;
use clap::Parser;

#[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,
},
Expand Down Expand Up @@ -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));
Expand Down
14 changes: 6 additions & 8 deletions codespan-reporting/examples/reusable_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ use codespan_reporting::files::SimpleFile;
use codespan_reporting::term::termcolor::StandardStream;
use codespan_reporting::term::{self, ColorArg};
use std::ops::Range;
use structopt::StructOpt;
use clap::Parser;

#[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,
}
Expand All @@ -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) {
Expand Down
14 changes: 6 additions & 8 deletions codespan-reporting/examples/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ 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;
use clap::Parser;

#[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(
Expand Down
22 changes: 10 additions & 12 deletions codespan-reporting/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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"];
}

Expand Down

0 comments on commit e4e65da

Please sign in to comment.