refactor: Rename CLI arg structs from Cli to Args

Technically the command-line interface (CLI) encompasses both input and
output, so naming the input structs 'Args' is more accurate than 'Cli'.
This commit is contained in:
Martin Fischer
2023-01-25 03:53:40 +01:00
committed by Charlie Marsh
parent b346f74915
commit d9ead4e6df
15 changed files with 86 additions and 95 deletions

View File

@@ -1,29 +1,28 @@
//! Run all code and documentation generation steps.
use anyhow::Result;
use clap::Args;
use crate::{generate_cli_help, generate_json_schema, generate_options, generate_rules_table};
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Write the generated artifacts to stdout (rather than to the filesystem).
#[arg(long)]
dry_run: bool,
}
pub fn main(cli: &Cli) -> Result<()> {
generate_json_schema::main(&generate_json_schema::Cli {
dry_run: cli.dry_run,
pub fn main(args: &Args) -> Result<()> {
generate_json_schema::main(&generate_json_schema::Args {
dry_run: args.dry_run,
})?;
generate_rules_table::main(&generate_rules_table::Cli {
dry_run: cli.dry_run,
generate_rules_table::main(&generate_rules_table::Args {
dry_run: args.dry_run,
})?;
generate_options::main(&generate_options::Cli {
dry_run: cli.dry_run,
generate_options::main(&generate_options::Args {
dry_run: args.dry_run,
})?;
generate_cli_help::main(&generate_cli_help::Cli {
dry_run: cli.dry_run,
generate_cli_help::main(&generate_cli_help::Args {
dry_run: args.dry_run,
})?;
Ok(())
}

View File

@@ -1,15 +1,14 @@
//! Generate CLI help.
use anyhow::Result;
use clap::Args;
use crate::utils::replace_readme_section;
const HELP_BEGIN_PRAGMA: &str = "<!-- Begin auto-generated cli help. -->";
const HELP_END_PRAGMA: &str = "<!-- End auto-generated cli help. -->";
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Write the generated help to stdout (rather than to `README.md`).
#[arg(long)]
pub(crate) dry_run: bool,
@@ -19,10 +18,10 @@ fn trim_lines(s: &str) -> String {
s.lines().map(str::trim_end).collect::<Vec<_>>().join("\n")
}
pub fn main(cli: &Cli) -> Result<()> {
pub fn main(args: &Args) -> Result<()> {
let output = trim_lines(ruff_cli::help().trim());
if cli.dry_run {
if args.dry_run {
print!("{output}");
} else {
replace_readme_section(

View File

@@ -2,22 +2,21 @@ use std::fs;
use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use ruff::settings::options::Options;
use schemars::schema_for;
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Write the generated table to stdout (rather than to `ruff.schema.json`).
#[arg(long)]
pub(crate) dry_run: bool,
}
pub fn main(cli: &Cli) -> Result<()> {
pub fn main(args: &Args) -> Result<()> {
let schema = schema_for!(Options);
let schema_string = serde_json::to_string_pretty(&schema).unwrap();
if cli.dry_run {
if args.dry_run {
println!("{schema_string}");
} else {
let file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))

View File

@@ -1,7 +1,6 @@
//! Generate a Markdown-compatible listing of configuration options.
use anyhow::Result;
use clap::Args;
use itertools::Itertools;
use ruff::settings::options::Options;
use ruff::settings::options_base::{ConfigurationOptions, OptionEntry, OptionField};
@@ -11,8 +10,8 @@ use crate::utils::replace_readme_section;
const BEGIN_PRAGMA: &str = "<!-- Begin auto-generated options sections. -->";
const END_PRAGMA: &str = "<!-- End auto-generated options sections. -->";
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Write the generated table to stdout (rather than to `README.md`).
#[arg(long)]
pub(crate) dry_run: bool,
@@ -39,7 +38,7 @@ fn emit_field(output: &mut String, field: &OptionField, group_name: Option<&str>
output.push('\n');
}
pub fn main(cli: &Cli) -> Result<()> {
pub fn main(args: &Args) -> Result<()> {
let mut output = String::new();
// Generate all the top-level fields.
@@ -89,7 +88,7 @@ pub fn main(cli: &Cli) -> Result<()> {
}
}
if cli.dry_run {
if args.dry_run {
print!("{output}");
} else {
replace_readme_section(&output, BEGIN_PRAGMA, END_PRAGMA)?;

View File

@@ -1,7 +1,6 @@
//! Generate a Markdown-compatible table of supported lint rules.
use anyhow::Result;
use clap::Args;
use ruff::registry::{Linter, LinterCategory, Rule, RuleNamespace};
use strum::IntoEnumIterator;
@@ -13,8 +12,8 @@ const TABLE_END_PRAGMA: &str = "<!-- End auto-generated sections. -->";
const TOC_BEGIN_PRAGMA: &str = "<!-- Begin auto-generated table of contents. -->";
const TOC_END_PRAGMA: &str = "<!-- End auto-generated table of contents. -->";
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Write the generated table to stdout (rather than to `README.md`).
#[arg(long)]
pub(crate) dry_run: bool,
@@ -43,7 +42,7 @@ fn generate_table(table_out: &mut String, rules: impl IntoIterator<Item = Rule>)
table_out.push('\n');
}
pub fn main(cli: &Cli) -> Result<()> {
pub fn main(args: &Args) -> Result<()> {
// Generate the table string.
let mut table_out = String::new();
let mut toc_out = String::new();
@@ -96,7 +95,7 @@ pub fn main(cli: &Cli) -> Result<()> {
}
}
if cli.dry_run {
if args.dry_run {
print!("Table of Contents: {toc_out}\n Rules Tables: {table_out}");
} else {
// Extra newline in the markdown numbered list looks weird

View File

@@ -33,45 +33,45 @@ use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
struct Args {
#[command(subcommand)]
command: Commands,
command: Command,
}
#[derive(Subcommand)]
enum Commands {
enum Command {
/// Run all code and documentation generation steps.
GenerateAll(generate_all::Cli),
GenerateAll(generate_all::Args),
/// Generate JSON schema for the TOML configuration file.
GenerateJSONSchema(generate_json_schema::Cli),
GenerateJSONSchema(generate_json_schema::Args),
/// Generate a Markdown-compatible table of supported lint rules.
GenerateRulesTable(generate_rules_table::Cli),
GenerateRulesTable(generate_rules_table::Args),
/// Generate a Markdown-compatible listing of configuration options.
GenerateOptions(generate_options::Cli),
GenerateOptions(generate_options::Args),
/// Generate CLI help.
GenerateCliHelp(generate_cli_help::Cli),
GenerateCliHelp(generate_cli_help::Args),
/// Print the AST for a given Python file.
PrintAST(print_ast::Cli),
PrintAST(print_ast::Args),
/// Print the LibCST CST for a given Python file.
PrintCST(print_cst::Cli),
PrintCST(print_cst::Args),
/// Print the token stream for a given Python file.
PrintTokens(print_tokens::Cli),
PrintTokens(print_tokens::Args),
/// Run round-trip source code generation on a given Python file.
RoundTrip(round_trip::Cli),
RoundTrip(round_trip::Args),
}
fn main() -> Result<()> {
let cli = Cli::parse();
match &cli.command {
Commands::GenerateAll(args) => generate_all::main(args)?,
Commands::GenerateJSONSchema(args) => generate_json_schema::main(args)?,
Commands::GenerateRulesTable(args) => generate_rules_table::main(args)?,
Commands::GenerateOptions(args) => generate_options::main(args)?,
Commands::GenerateCliHelp(args) => generate_cli_help::main(args)?,
Commands::PrintAST(args) => print_ast::main(args)?,
Commands::PrintCST(args) => print_cst::main(args)?,
Commands::PrintTokens(args) => print_tokens::main(args)?,
Commands::RoundTrip(args) => round_trip::main(args)?,
let args = Args::parse();
match &args.command {
Command::GenerateAll(args) => generate_all::main(args)?,
Command::GenerateJSONSchema(args) => generate_json_schema::main(args)?,
Command::GenerateRulesTable(args) => generate_rules_table::main(args)?,
Command::GenerateOptions(args) => generate_options::main(args)?,
Command::GenerateCliHelp(args) => generate_cli_help::main(args)?,
Command::PrintAST(args) => print_ast::main(args)?,
Command::PrintCST(args) => print_cst::main(args)?,
Command::PrintTokens(args) => print_tokens::main(args)?,
Command::RoundTrip(args) => round_trip::main(args)?,
}
Ok(())
}

View File

@@ -4,19 +4,18 @@ use std::fs;
use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use rustpython_parser::parser;
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Python file for which to generate the AST.
#[arg(required = true)]
file: PathBuf,
}
pub fn main(cli: &Cli) -> Result<()> {
let contents = fs::read_to_string(&cli.file)?;
let python_ast = parser::parse_program(&contents, &cli.file.to_string_lossy())?;
pub fn main(args: &Args) -> Result<()> {
let contents = fs::read_to_string(&args.file)?;
let python_ast = parser::parse_program(&contents, &args.file.to_string_lossy())?;
println!("{python_ast:#?}");
Ok(())
}

View File

@@ -4,17 +4,16 @@ use std::fs;
use std::path::PathBuf;
use anyhow::{bail, Result};
use clap::Args;
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Python file for which to generate the CST.
#[arg(required = true)]
file: PathBuf,
}
pub fn main(cli: &Cli) -> Result<()> {
let contents = fs::read_to_string(&cli.file)?;
pub fn main(args: &Args) -> Result<()> {
let contents = fs::read_to_string(&args.file)?;
match libcst_native::parse_module(&contents, None) {
Ok(python_cst) => {
println!("{python_cst:#?}");

View File

@@ -4,18 +4,17 @@ use std::fs;
use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use rustpython_parser::lexer;
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Python file for which to generate the AST.
#[arg(required = true)]
file: PathBuf,
}
pub fn main(cli: &Cli) -> Result<()> {
let contents = fs::read_to_string(&cli.file)?;
pub fn main(args: &Args) -> Result<()> {
let contents = fs::read_to_string(&args.file)?;
for (_, tok, _) in lexer::make_tokenizer(&contents).flatten() {
println!("{tok:#?}");
}

View File

@@ -4,18 +4,17 @@ use std::fs;
use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use ruff::source_code::round_trip;
#[derive(Args)]
pub struct Cli {
#[derive(clap::Args)]
pub struct Args {
/// Python file to round-trip.
#[arg(required = true)]
file: PathBuf,
}
pub fn main(cli: &Cli) -> Result<()> {
let contents = fs::read_to_string(&cli.file)?;
println!("{}", round_trip(&contents, &cli.file.to_string_lossy())?);
pub fn main(args: &Args) -> Result<()> {
let contents = fs::read_to_string(&args.file)?;
println!("{}", round_trip(&contents, &args.file.to_string_lossy())?);
Ok(())
}