class Analyzer::Rust::Cli

Overview

Surfaces the command-line attack surface of Rust programs as cli:// endpoints: one endpoint per (sub)command with named options (param_type "flag"), positional arguments ("argument") and consumed environment variables ("env"). Covers std::env::args/var plus the clap / structopt / argh derive macros and the getopts builder API (Options::new() + opts.optopt/optflag/optflagopt/optmulti/reqopt).

Scope notes (follow-ups): the clap builder API (Command::new(...) .arg(Arg::new(...))) is only recognized as a CLI signal, not parsed into args — a line-scan can't reliably track its method-chain/paren scoping. Tuple subcommand variants that reference a separate #[derive(Args)] struct (Serve(ServeArgs)) surface that struct's flags on the root rather than the subcommand, since the variant→struct link isn't resolved here; inline-field variants (Serve { ... }) attribute correctly.

Line-scan analyzer (Go/Python CLI house style) with a cross-file URL-merge, instead of the tree-sitter analyze_file path the HTTP Rust analyzers use. Subclasses Analyzer directly (RustEngine#analyze_file is abstract) and reuses RustEngine.test_path? as a class method.

Defined in:

analyzer/analyzers/rust/cli.cr

Constant Summary

ARGH_ATTR_RE = /#\[\s*argh\s*\(([^\]]*)\)/
BUILDER_NEW = /Command::new\s*\(\s*"([^"]+)"/

clap builder marker (used only to recognise a builder-style CLI; the builder arg/subcommand tree is a follow-up — see the note in #analyze).

CLAP_ATTR_RE = /#\[\s*(?:arg|structopt|clap)\s*\(([^\]]*)\)/

clap/structopt field attribute, e.g. #[arg(short, long, env = "X")].

DERIVE_RE = /#\[\s*derive\s*\(([^)]*)\)/
ENV_ARGS_RE = /\b(?:std::)?env::args(?:_os)?\s*\(/
ENV_VAR_RE = /\b(?:std::)?env::var(?:_os)?\s*\(\s*"([^"]+)"/

builtin.

FIELD_RE = /^\s*(?:pub\s+)?(\w+)\s*:/
GETOPTS_CALL_RE = /\b(\w+)\.(?:optopt|reqopt|optflagopt|optflag|optmulti)\s*\(\s*"([^"]*)"\s*,\s*"([^"]*)"/
GETOPTS_NEW_RE = /\blet\s+(?:mut\s+)?(\w+)(?:\s*:\s*(?:getopts::)?Options)?\s*=\s*(?:getopts::)?Options::new\s*\(\s*\)/

getopts: a flat (no-subcommand) builder API. let mut opts = Options::new(); binds a receiver variable, then opts.optopt(short, long, desc, hint) / optflag(short, long, desc) / optflagopt(short, long, desc, hint) / optmulti(short, long, desc, hint) / reqopt(short, long, desc, hint) register each option on it. The optional type-annotation group tolerates a getopts:: prefix (let mut opts: getopts::Options = ...), matching the same prefix already tolerated before Options::new.

ITEM_RE = /\b(?:struct|enum)\s+\w+/
VARIANT_RE = /^\s*(?:#\[\s*command\s*\(\s*name\s*=\s*"([^"]+)"[^\]]*\)\s*\]\s*)?([A-Z]\w*)/
WEB_CRATE_RE = /\buse\s+(?:axum|actix_web|rocket|warp|tide|poem|salvo|gotham|loco_rs|hyper|tonic|tower_http)\b|::serve\s*\(|HttpServer::new|TcpListener::bind/

Web crates: their env::var reads are config, not a CLI surface.

Instance Method Summary

Instance methods inherited from class Analyzer

analyze analyze, base_path : String base_path, base_paths : Array(String) base_paths, callees_needed? : Bool callees_needed?, http_header_name(name : String) : String | Nil http_header_name, line_number_for_index(content : String, char_index : Int32) : Int32 line_number_for_index, logger : NoirLogger logger, parallel_analyze(files : Array(String), &block : String -> Nil) parallel_analyze, read_file_content(path : String) : String read_file_content, result : Array(Endpoint) result, unique_params(params : Array(Param)) : Array(Param) unique_params, url : String url, web_root_path(path : String, markers : Array(String)) : String web_root_path

Constructor methods inherited from class Analyzer

new(options : Hash(String, YAML::Any)) new

Instance methods inherited from module FileHelper

all_files : Array(String) all_files, get_files_by_extension(extension : String) : Array(String) get_files_by_extension, get_files_by_extensions(extensions : Array(String)) : Array(String) get_files_by_extensions, get_files_by_prefix(prefix : String) : Array(String) get_files_by_prefix, get_files_by_prefix_and_extension(prefix : String, extension : String) : Array(String) get_files_by_prefix_and_extension, get_public_dir_files(base_path : String, folder : String) : Array(String) get_public_dir_files, get_public_files(base_path : String, anchors : Array(String) = ["shard.yml", "Gemfile"]) : Array(String) get_public_files

Instance Method Detail

def analyze #

[View source]