mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00
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:
parent
b346f74915
commit
d9ead4e6df
15 changed files with 86 additions and 95 deletions
|
@ -26,7 +26,7 @@ use ruff::flake8_to_ruff::{self, ExternalConfig};
|
||||||
about = "Convert existing Flake8 configuration to Ruff.",
|
about = "Convert existing Flake8 configuration to Ruff.",
|
||||||
long_about = None
|
long_about = None
|
||||||
)]
|
)]
|
||||||
struct Cli {
|
struct Args {
|
||||||
/// Path to the Flake8 configuration file (e.g., `setup.cfg`, `tox.ini`, or
|
/// Path to the Flake8 configuration file (e.g., `setup.cfg`, `tox.ini`, or
|
||||||
/// `.flake8`).
|
/// `.flake8`).
|
||||||
#[arg(required = true)]
|
#[arg(required = true)]
|
||||||
|
@ -41,15 +41,15 @@ struct Cli {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let cli = Cli::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
// Read the INI file.
|
// Read the INI file.
|
||||||
let mut ini = Ini::new_cs();
|
let mut ini = Ini::new_cs();
|
||||||
ini.set_multiline(true);
|
ini.set_multiline(true);
|
||||||
let config = ini.load(cli.file).map_err(|msg| anyhow::anyhow!(msg))?;
|
let config = ini.load(args.file).map_err(|msg| anyhow::anyhow!(msg))?;
|
||||||
|
|
||||||
// Read the pyproject.toml file.
|
// Read the pyproject.toml file.
|
||||||
let pyproject = cli.pyproject.map(flake8_to_ruff::parse).transpose()?;
|
let pyproject = args.pyproject.map(flake8_to_ruff::parse).transpose()?;
|
||||||
let external_config = pyproject
|
let external_config = pyproject
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|pyproject| pyproject.tool.as_ref())
|
.and_then(|pyproject| pyproject.tool.as_ref())
|
||||||
|
@ -60,7 +60,7 @@ fn main() -> Result<()> {
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
// Create Ruff's pyproject.toml section.
|
// Create Ruff's pyproject.toml section.
|
||||||
let pyproject = flake8_to_ruff::convert(&config, &external_config, cli.plugin)?;
|
let pyproject = flake8_to_ruff::convert(&config, &external_config, args.plugin)?;
|
||||||
println!("{}", toml::to_string_pretty(&pyproject)?);
|
println!("{}", toml::to_string_pretty(&pyproject)?);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -19,7 +19,7 @@ use rustc_hash::FxHashMap;
|
||||||
)]
|
)]
|
||||||
#[command(version)]
|
#[command(version)]
|
||||||
#[allow(clippy::struct_excessive_bools)]
|
#[allow(clippy::struct_excessive_bools)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
#[arg(required_unless_present_any = ["clean", "explain", "generate_shell_completion"])]
|
#[arg(required_unless_present_any = ["clean", "explain", "generate_shell_completion"])]
|
||||||
pub files: Vec<PathBuf>,
|
pub files: Vec<PathBuf>,
|
||||||
/// Path to the `pyproject.toml` or `ruff.toml` file to use for
|
/// Path to the `pyproject.toml` or `ruff.toml` file to use for
|
||||||
|
@ -231,7 +231,7 @@ pub struct Cli {
|
||||||
pub show_settings: bool,
|
pub show_settings: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cli {
|
impl Args {
|
||||||
/// Partition the CLI into command-line arguments and configuration
|
/// Partition the CLI into command-line arguments and configuration
|
||||||
/// overrides.
|
/// overrides.
|
||||||
pub fn partition(self) -> (Arguments, Overrides) {
|
pub fn partition(self) -> (Arguments, Overrides) {
|
||||||
|
@ -421,12 +421,12 @@ impl ConfigProcessor for &Overrides {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map the CLI settings to a `LogLevel`.
|
/// Map the CLI settings to a `LogLevel`.
|
||||||
pub fn extract_log_level(cli: &Arguments) -> LogLevel {
|
pub fn extract_log_level(args: &Arguments) -> LogLevel {
|
||||||
if cli.silent {
|
if args.silent {
|
||||||
LogLevel::Silent
|
LogLevel::Silent
|
||||||
} else if cli.quiet {
|
} else if args.quiet {
|
||||||
LogLevel::Quiet
|
LogLevel::Quiet
|
||||||
} else if cli.verbose {
|
} else if args.verbose {
|
||||||
LogLevel::Verbose
|
LogLevel::Verbose
|
||||||
} else {
|
} else {
|
||||||
LogLevel::Default
|
LogLevel::Default
|
|
@ -23,8 +23,8 @@ use ruff::{fix, fs, packaging, resolver, warn_user_once, AutofixAvailability, IO
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
|
use crate::args::Overrides;
|
||||||
use crate::cache;
|
use crate::cache;
|
||||||
use crate::cli::Overrides;
|
|
||||||
use crate::diagnostics::{lint_path, lint_stdin, Diagnostics};
|
use crate::diagnostics::{lint_path, lint_stdin, Diagnostics};
|
||||||
use crate::iterators::par_iter;
|
use crate::iterators::par_iter;
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,11 @@
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
#![allow(clippy::must_use_candidate, dead_code)]
|
#![allow(clippy::must_use_candidate, dead_code)]
|
||||||
|
|
||||||
mod cli;
|
mod args;
|
||||||
|
|
||||||
use clap::CommandFactory;
|
use clap::CommandFactory;
|
||||||
|
|
||||||
/// Returns the output of `ruff --help`.
|
/// Returns the output of `ruff --help`.
|
||||||
pub fn help() -> String {
|
pub fn help() -> String {
|
||||||
cli::Cli::command().render_help().to_string()
|
args::Args::command().render_help().to_string()
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,16 +21,16 @@ use ::ruff::settings::pyproject;
|
||||||
use ::ruff::settings::types::SerializationFormat;
|
use ::ruff::settings::types::SerializationFormat;
|
||||||
use ::ruff::{fix, fs, warn_user_once};
|
use ::ruff::{fix, fs, warn_user_once};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use args::{extract_log_level, Args, Overrides};
|
||||||
use clap::{CommandFactory, Parser};
|
use clap::{CommandFactory, Parser};
|
||||||
use cli::{extract_log_level, Cli, Overrides};
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use notify::{recommended_watcher, RecursiveMode, Watcher};
|
use notify::{recommended_watcher, RecursiveMode, Watcher};
|
||||||
use path_absolutize::path_dedot;
|
use path_absolutize::path_dedot;
|
||||||
use printer::{Printer, Violations};
|
use printer::{Printer, Violations};
|
||||||
use ruff::settings::{AllSettings, CliSettings};
|
use ruff::settings::{AllSettings, CliSettings};
|
||||||
|
|
||||||
|
mod args;
|
||||||
mod cache;
|
mod cache;
|
||||||
mod cli;
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod diagnostics;
|
mod diagnostics;
|
||||||
mod iterators;
|
mod iterators;
|
||||||
|
@ -91,7 +91,7 @@ fn resolve(
|
||||||
|
|
||||||
fn inner_main() -> Result<ExitCode> {
|
fn inner_main() -> Result<ExitCode> {
|
||||||
// Extract command-line arguments.
|
// Extract command-line arguments.
|
||||||
let (cli, overrides) = Cli::parse().partition();
|
let (cli, overrides) = Args::parse().partition();
|
||||||
|
|
||||||
let default_panic_hook = std::panic::take_hook();
|
let default_panic_hook = std::panic::take_hook();
|
||||||
std::panic::set_hook(Box::new(move |info| {
|
std::panic::set_hook(Box::new(move |info| {
|
||||||
|
@ -112,7 +112,7 @@ quoting the executed command, along with the relevant file contents and `pyproje
|
||||||
set_up_logging(&log_level)?;
|
set_up_logging(&log_level)?;
|
||||||
|
|
||||||
if let Some(shell) = cli.generate_shell_completion {
|
if let Some(shell) = cli.generate_shell_completion {
|
||||||
shell.generate(&mut Cli::command(), &mut io::stdout());
|
shell.generate(&mut Args::command(), &mut io::stdout());
|
||||||
return Ok(ExitCode::SUCCESS);
|
return Ok(ExitCode::SUCCESS);
|
||||||
}
|
}
|
||||||
if cli.clean {
|
if cli.clean {
|
||||||
|
|
|
@ -1,29 +1,28 @@
|
||||||
//! Run all code and documentation generation steps.
|
//! Run all code and documentation generation steps.
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
|
|
||||||
use crate::{generate_cli_help, generate_json_schema, generate_options, generate_rules_table};
|
use crate::{generate_cli_help, generate_json_schema, generate_options, generate_rules_table};
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Write the generated artifacts to stdout (rather than to the filesystem).
|
/// Write the generated artifacts to stdout (rather than to the filesystem).
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
generate_json_schema::main(&generate_json_schema::Cli {
|
generate_json_schema::main(&generate_json_schema::Args {
|
||||||
dry_run: cli.dry_run,
|
dry_run: args.dry_run,
|
||||||
})?;
|
})?;
|
||||||
generate_rules_table::main(&generate_rules_table::Cli {
|
generate_rules_table::main(&generate_rules_table::Args {
|
||||||
dry_run: cli.dry_run,
|
dry_run: args.dry_run,
|
||||||
})?;
|
})?;
|
||||||
generate_options::main(&generate_options::Cli {
|
generate_options::main(&generate_options::Args {
|
||||||
dry_run: cli.dry_run,
|
dry_run: args.dry_run,
|
||||||
})?;
|
})?;
|
||||||
generate_cli_help::main(&generate_cli_help::Cli {
|
generate_cli_help::main(&generate_cli_help::Args {
|
||||||
dry_run: cli.dry_run,
|
dry_run: args.dry_run,
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
//! Generate CLI help.
|
//! Generate CLI help.
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
|
|
||||||
use crate::utils::replace_readme_section;
|
use crate::utils::replace_readme_section;
|
||||||
|
|
||||||
const HELP_BEGIN_PRAGMA: &str = "<!-- Begin auto-generated cli help. -->";
|
const HELP_BEGIN_PRAGMA: &str = "<!-- Begin auto-generated cli help. -->";
|
||||||
const HELP_END_PRAGMA: &str = "<!-- End auto-generated cli help. -->";
|
const HELP_END_PRAGMA: &str = "<!-- End auto-generated cli help. -->";
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Write the generated help to stdout (rather than to `README.md`).
|
/// Write the generated help to stdout (rather than to `README.md`).
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub(crate) dry_run: bool,
|
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")
|
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());
|
let output = trim_lines(ruff_cli::help().trim());
|
||||||
|
|
||||||
if cli.dry_run {
|
if args.dry_run {
|
||||||
print!("{output}");
|
print!("{output}");
|
||||||
} else {
|
} else {
|
||||||
replace_readme_section(
|
replace_readme_section(
|
||||||
|
|
|
@ -2,22 +2,21 @@ use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
use ruff::settings::options::Options;
|
use ruff::settings::options::Options;
|
||||||
use schemars::schema_for;
|
use schemars::schema_for;
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Write the generated table to stdout (rather than to `ruff.schema.json`).
|
/// Write the generated table to stdout (rather than to `ruff.schema.json`).
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub(crate) dry_run: bool,
|
pub(crate) dry_run: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
let schema = schema_for!(Options);
|
let schema = schema_for!(Options);
|
||||||
let schema_string = serde_json::to_string_pretty(&schema).unwrap();
|
let schema_string = serde_json::to_string_pretty(&schema).unwrap();
|
||||||
|
|
||||||
if cli.dry_run {
|
if args.dry_run {
|
||||||
println!("{schema_string}");
|
println!("{schema_string}");
|
||||||
} else {
|
} else {
|
||||||
let file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
let file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
//! Generate a Markdown-compatible listing of configuration options.
|
//! Generate a Markdown-compatible listing of configuration options.
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ruff::settings::options::Options;
|
use ruff::settings::options::Options;
|
||||||
use ruff::settings::options_base::{ConfigurationOptions, OptionEntry, OptionField};
|
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 BEGIN_PRAGMA: &str = "<!-- Begin auto-generated options sections. -->";
|
||||||
const END_PRAGMA: &str = "<!-- End auto-generated options sections. -->";
|
const END_PRAGMA: &str = "<!-- End auto-generated options sections. -->";
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Write the generated table to stdout (rather than to `README.md`).
|
/// Write the generated table to stdout (rather than to `README.md`).
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub(crate) dry_run: bool,
|
pub(crate) dry_run: bool,
|
||||||
|
@ -39,7 +38,7 @@ fn emit_field(output: &mut String, field: &OptionField, group_name: Option<&str>
|
||||||
output.push('\n');
|
output.push('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
|
|
||||||
// Generate all the top-level fields.
|
// 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}");
|
print!("{output}");
|
||||||
} else {
|
} else {
|
||||||
replace_readme_section(&output, BEGIN_PRAGMA, END_PRAGMA)?;
|
replace_readme_section(&output, BEGIN_PRAGMA, END_PRAGMA)?;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
//! Generate a Markdown-compatible table of supported lint rules.
|
//! Generate a Markdown-compatible table of supported lint rules.
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
use ruff::registry::{Linter, LinterCategory, Rule, RuleNamespace};
|
use ruff::registry::{Linter, LinterCategory, Rule, RuleNamespace};
|
||||||
use strum::IntoEnumIterator;
|
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_BEGIN_PRAGMA: &str = "<!-- Begin auto-generated table of contents. -->";
|
||||||
const TOC_END_PRAGMA: &str = "<!-- End auto-generated table of contents. -->";
|
const TOC_END_PRAGMA: &str = "<!-- End auto-generated table of contents. -->";
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Write the generated table to stdout (rather than to `README.md`).
|
/// Write the generated table to stdout (rather than to `README.md`).
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub(crate) dry_run: bool,
|
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');
|
table_out.push('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
// Generate the table string.
|
// Generate the table string.
|
||||||
let mut table_out = String::new();
|
let mut table_out = String::new();
|
||||||
let mut toc_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}");
|
print!("Table of Contents: {toc_out}\n Rules Tables: {table_out}");
|
||||||
} else {
|
} else {
|
||||||
// Extra newline in the markdown numbered list looks weird
|
// Extra newline in the markdown numbered list looks weird
|
||||||
|
|
|
@ -33,45 +33,45 @@ use clap::{Parser, Subcommand};
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
#[command(propagate_version = true)]
|
#[command(propagate_version = true)]
|
||||||
struct Cli {
|
struct Args {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
command: Commands,
|
command: Command,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Commands {
|
enum Command {
|
||||||
/// Run all code and documentation generation steps.
|
/// Run all code and documentation generation steps.
|
||||||
GenerateAll(generate_all::Cli),
|
GenerateAll(generate_all::Args),
|
||||||
/// Generate JSON schema for the TOML configuration file.
|
/// 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.
|
/// 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.
|
/// Generate a Markdown-compatible listing of configuration options.
|
||||||
GenerateOptions(generate_options::Cli),
|
GenerateOptions(generate_options::Args),
|
||||||
/// Generate CLI help.
|
/// Generate CLI help.
|
||||||
GenerateCliHelp(generate_cli_help::Cli),
|
GenerateCliHelp(generate_cli_help::Args),
|
||||||
/// Print the AST for a given Python file.
|
/// 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.
|
/// 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.
|
/// 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.
|
/// Run round-trip source code generation on a given Python file.
|
||||||
RoundTrip(round_trip::Cli),
|
RoundTrip(round_trip::Args),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let cli = Cli::parse();
|
let args = Args::parse();
|
||||||
match &cli.command {
|
match &args.command {
|
||||||
Commands::GenerateAll(args) => generate_all::main(args)?,
|
Command::GenerateAll(args) => generate_all::main(args)?,
|
||||||
Commands::GenerateJSONSchema(args) => generate_json_schema::main(args)?,
|
Command::GenerateJSONSchema(args) => generate_json_schema::main(args)?,
|
||||||
Commands::GenerateRulesTable(args) => generate_rules_table::main(args)?,
|
Command::GenerateRulesTable(args) => generate_rules_table::main(args)?,
|
||||||
Commands::GenerateOptions(args) => generate_options::main(args)?,
|
Command::GenerateOptions(args) => generate_options::main(args)?,
|
||||||
Commands::GenerateCliHelp(args) => generate_cli_help::main(args)?,
|
Command::GenerateCliHelp(args) => generate_cli_help::main(args)?,
|
||||||
Commands::PrintAST(args) => print_ast::main(args)?,
|
Command::PrintAST(args) => print_ast::main(args)?,
|
||||||
Commands::PrintCST(args) => print_cst::main(args)?,
|
Command::PrintCST(args) => print_cst::main(args)?,
|
||||||
Commands::PrintTokens(args) => print_tokens::main(args)?,
|
Command::PrintTokens(args) => print_tokens::main(args)?,
|
||||||
Commands::RoundTrip(args) => round_trip::main(args)?,
|
Command::RoundTrip(args) => round_trip::main(args)?,
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,19 +4,18 @@ use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
use rustpython_parser::parser;
|
use rustpython_parser::parser;
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Python file for which to generate the AST.
|
/// Python file for which to generate the AST.
|
||||||
#[arg(required = true)]
|
#[arg(required = true)]
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
let contents = fs::read_to_string(&cli.file)?;
|
let contents = fs::read_to_string(&args.file)?;
|
||||||
let python_ast = parser::parse_program(&contents, &cli.file.to_string_lossy())?;
|
let python_ast = parser::parse_program(&contents, &args.file.to_string_lossy())?;
|
||||||
println!("{python_ast:#?}");
|
println!("{python_ast:#?}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,16 @@ use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use clap::Args;
|
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Python file for which to generate the CST.
|
/// Python file for which to generate the CST.
|
||||||
#[arg(required = true)]
|
#[arg(required = true)]
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
let contents = fs::read_to_string(&cli.file)?;
|
let contents = fs::read_to_string(&args.file)?;
|
||||||
match libcst_native::parse_module(&contents, None) {
|
match libcst_native::parse_module(&contents, None) {
|
||||||
Ok(python_cst) => {
|
Ok(python_cst) => {
|
||||||
println!("{python_cst:#?}");
|
println!("{python_cst:#?}");
|
||||||
|
|
|
@ -4,18 +4,17 @@ use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
use rustpython_parser::lexer;
|
use rustpython_parser::lexer;
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Python file for which to generate the AST.
|
/// Python file for which to generate the AST.
|
||||||
#[arg(required = true)]
|
#[arg(required = true)]
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
let contents = fs::read_to_string(&cli.file)?;
|
let contents = fs::read_to_string(&args.file)?;
|
||||||
for (_, tok, _) in lexer::make_tokenizer(&contents).flatten() {
|
for (_, tok, _) in lexer::make_tokenizer(&contents).flatten() {
|
||||||
println!("{tok:#?}");
|
println!("{tok:#?}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,18 +4,17 @@ use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Args;
|
|
||||||
use ruff::source_code::round_trip;
|
use ruff::source_code::round_trip;
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(clap::Args)]
|
||||||
pub struct Cli {
|
pub struct Args {
|
||||||
/// Python file to round-trip.
|
/// Python file to round-trip.
|
||||||
#[arg(required = true)]
|
#[arg(required = true)]
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(cli: &Cli) -> Result<()> {
|
pub fn main(args: &Args) -> Result<()> {
|
||||||
let contents = fs::read_to_string(&cli.file)?;
|
let contents = fs::read_to_string(&args.file)?;
|
||||||
println!("{}", round_trip(&contents, &cli.file.to_string_lossy())?);
|
println!("{}", round_trip(&contents, &args.file.to_string_lossy())?);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue