From 6e593f8ac99eb0783cd1690e469b237b59ae0132 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sun, 14 Dec 2025 03:37:03 +0100 Subject: [PATCH] Clap shenenigans --- bin_efo/src/commands/entity_generic.rs | 211 ++++++++++-------- .../entity_generic/helper_functions.rs | 17 +- 2 files changed, 132 insertions(+), 96 deletions(-) diff --git a/bin_efo/src/commands/entity_generic.rs b/bin_efo/src/commands/entity_generic.rs index 18147bf..1e530d8 100644 --- a/bin_efo/src/commands/entity_generic.rs +++ b/bin_efo/src/commands/entity_generic.rs @@ -20,17 +20,21 @@ //! //! ### Options //! -//! | Flag | Description | -//! |------|-------------| -//! | `-d, --delta` | Apply a delta to the supplied stats (add or subtract) instead of overwriting them. | -//! | `-v, --verbose…` | Increase output verbosity. | -//! | `--rng ` | How to fill unspecified stats.
Possible values: `zero`, `low`, `med`, `high`, `legendary`, `mixed`, `flawed`. Default: `zero`. | -//! | `--pos-exact ` / `--pos ` | Place the new entity at a specific `x,y` coordinate (the epoch must be in a map context). | -//! | `--duration ` | Lifespan of the entity (e.g. `Permanent`, `Temporary`, `Duration(1)` for a one‑turn event). | -//! | `--bounds ` | Effect limits, given as `min,max` (e.g. `10,80`). | -//! | `--effect ` | How the effect is applied:
`recurring`, `when-present`, `permanent-effect-to-apply`, `permanent-effect-applied`. | -//! | `-s, --stats ` | One or more `key:value` pairs, e.g. `fod,10 military:8`. | +//! | Flag | Group | Description | +//! |------|-------|-------------| +//! | `-d, --delta` | *edit* | Treat the supplied `stats` values as a delta (add/subtract) +//! instead of an absolute overwrite. | +//! | `-v, --verbose…` | *global* | Increases the amount of diagnostic output. | +//! | `--rng ` | *add/edit* | How to initialise *missing* stats. Possible values: `zero`, `low`, `med`, `high`, `legendary`, `mixed`, `flawed`. Default: `zero`. | +//! | `--pos-exact ` / `--pos ` | *add/edit* | Set the entity’s coordinates. `--pos-exact` forces the coordinates to be used verbatim; `--pos` applies the map’s current position transformation (if any). | +//! | `--duration ` | *add/edit* | Lifespan of the entity – e.g. `Permanent`, `Temporary` or `Duration(1)` for a one‑turn event. | +//! | `--bounds ` | *add/edit* | Effect limits in the form `min,max` (e.g. `10,80`). | +//! | `--effect ` | *add/edit* | How the effect is applied: `recurring`, `when-present`, `permanent-effect-to-apply`, `permanent-effect-applied`. | +//! | `-s, --stats ` | *add/edit* | One or more `key:value` pairs – e.g. `fod:10 military:8`. | +//! | `--rng-name` | *add* | If set, the tool generates a random name instead of using the supplied identifier. | //! +//! All changes are **persisted immediately** to the epoch file after the +//! command completes.//! //! ## Usage //! All the following example use `chr` as a choice of entity but can be safely substitued to any entity: `event`, `town`, `blocker` etc... //! @@ -81,106 +85,141 @@ use crate::success::display_success; mod helper_functions; -/// Commands that operate on an entity. -/// -/// Each variant represents a different operation that can be performed on an -/// entity. The variant holds the arguments that are common to all operations. +/// All operations that can be performed on an entity. +/// +/// Each variant contains the arguments that are common to that +/// operation. The enum itself is used by `clap` to dispatch +/// the user‑provided sub‑command. #[derive(Subcommand, Debug)] pub(crate) enum EntitySubCommands { - /// Add an entity - Add(EntityArgs), - /// Delete an entity - Del(DelArgs), - /// Edit an existing entity - Edit(EntityArgs), // /// Find an entity by its identifier // Find(EntityArgs), - /// Show the details of an entity + /// Create a new entity + Add(AddArgs), + /// Remove an existing entity + Del(DelArgs), + /// Update fields of an existing entity + Edit(EditArgs), + /// Show one or more entities Show(ShowArgs), - /// Show differences between two entities + /// Show the difference between two epochs Diff(DiffArgs), } +/// Arguments used only for the `diff` sub‑command. #[derive(Args, Debug)] -/// Arguments reserved for diffing pub(crate) struct DiffArgs { - /// The identifier either a name or a @tag - identifier: EntityIdentifier, + /// Identifier (name or `@tag`) of the entity to diff + pub identifier: EntityIdentifier, - /// Another epoch than the previous - epoch_id: Option, + /// Optional epoch to compare against. + /// If omitted the previous epoch is used. + #[arg(long, short = 'i')] + pub epoch_id: Option, } +/// Arguments for deleting an entity. #[derive(Args, Debug)] -/// Arguments required for any entity deletion pub(crate) struct DelArgs { - /// The identifier either a name or a @tag - identifier: EntityIdentifier, + /// Identifier (name or `@tag`) of the entity to delete + pub identifier: EntityIdentifier, } +/// Arguments for showing entities. #[derive(Args, Debug)] -/// Arguments required for showing any entity pub(crate) struct ShowArgs { - /// The identifier either a name or a @tag - identifier: Option, - - #[arg(long, short)] - /// Show only if belonging to faction - faction: Option, - // /// Show only if in a specific town - // #[arg(long, short)] - // town: Option, - // // - // /// Show only if not done - // #[arg(long, short, action = clap::ArgAction::SetTrue, default_value_t = Some(false))] - // valid: Option, -} - -/// Arguments required for any entity operation. -/// -/// All fields are required. The `rng` field determines how unspecified stat -/// values are handled when an entity is created or edited. -#[derive(Args, Debug)] -pub struct EntityArgs { - /// The identifier either a name or a @tag + /// Optional identifier – when omitted all entities are listed. pub identifier: Option, - // /// A name to change to - // #[arg(long, group = "identifier")] - // pub new_name: Option, - /// Applies a delta to stats instead of setting the values - #[arg(long, short, action = clap::ArgAction::SetTrue, default_value_t = false)] - pub delta: bool, + /// Filter by faction id (used only when an identifier is supplied) + #[arg(long, short)] + pub faction: Option, +} - /// The type of rng stat repartition - #[arg(value_enum, long, default_value_t = RngLevel::Zero)] +/// Common arguments used for *add* and *edit* operations. +#[derive(Args, Debug)] +pub struct AddArgs { + /// The desired name of the new entity. If omitted a random + /// name will be generated unless `--rng-name` is set. + #[arg(group = "name")] + pub identifier: Option, + + /// If true a random name will be generated *regardless* of + /// `identifier`. Useful when you want a quick anonymous test. + #[arg(long, action = clap::ArgAction::SetTrue, default_value_t = false, group = "name")] + pub rng_name: bool, + + /// How to initialise *missing* stats. See the `--rng` option + /// description for the valid values. + #[arg(value_enum, long, short, default_value_t = RngLevel::Zero)] pub rng: RngLevel, - /// Adds at a specific position - #[arg(long, group = "position")] + /// Place the new entity at an exact position – this ignores + /// any map‑specific coordinate transformations. + #[arg(long, short = 'P', group = "position")] pub pos_exact: Option, - /// Adds at a specific position - #[arg(long, group = "position")] + /// Place the new entity using the map’s position rules. + #[arg(long, short = 'p', group = "position")] pub pos: Option, - /// The duration of the entity - #[arg(long)] + /// Lifespan of the entity – see the `--duration` description. + #[arg(long, short)] pub duration: Option, - /// The bounds of the effect of the entity - #[arg(long)] + /// Effect bounds in the form `min,max`. See the `--bounds` + /// description for details. + #[arg(long, short)] pub bounds: Option, - /// How is the effect of the entity applied - #[arg(long)] + /// How the effect is applied – see the `--effect` description. + #[arg(long, short)] pub effect: Option, - /// All stats pairs involved + /// One or more `key:value` pairs that set the entity’s stats. #[arg(short, long)] pub stats: Vec, } +#[derive(Args, Debug)] +pub struct EditArgs { + /// Identifier (name or `@tag`) of the entity to edit + pub identifier: EntityIdentifier, + + /// New name for the entity (optional). + #[arg(long, short)] + pub new_name: Option, + + /// Treat the supplied `stats` as a delta (add/subtract) + /// rather than an absolute overwrite. + #[arg(long, short='D', action = clap::ArgAction::SetTrue, default_value_t = false)] + pub delta: bool, + + /// Update the entity’s exact position – ignores map transforms. + #[arg(long, short = 'P', group = "position")] + pub pos_exact: Option, + + /// Update the entity’s position using the map rules. + #[arg(long, short = 'p', group = "position")] + pub pos: Option, + + /// New duration for the entity. + #[arg(long, short)] + pub duration: Option, + + /// New effect bounds. + #[arg(long, short)] + pub bounds: Option, + + /// New effect application mode. + #[arg(short, long)] + pub effect: Option, + + /// One or more `key:value` pairs that modify the entity’s stats. + #[arg(short, long, num_args = 1..)] + pub stats: Vec, +} + /// Handles an entity subcommand. /// /// This function loads the main epoch configuration, then dispatches the @@ -223,22 +262,21 @@ pub fn handle_entity_commands( /// Function handler for the create subcommand pub(crate) fn handle_create_entity( - entity_args: &EntityArgs, + entity_args: &AddArgs, e_type: &EntityType, main_epoch: &mut EpochConfig, ) -> Result<(), EpochError> { let epoch_info = main_epoch.try_load_current_as_mut()?; + let ident = entity_args.identifier.clone().map(EntityIdentifier::Name); - if helper_functions::find_entity(&entity_args.identifier, e_type, epoch_info).is_ok() { - Err(EpochError::EntityAlreadyExist( - entity_args.identifier.clone(), - ))?; + if helper_functions::find_entity(&ident, e_type, epoch_info).is_ok() { + Err(EpochError::EntityAlreadyExist(ident))?; }; let vec_entities: &mut Vec = helper_functions::vec_entities_mut(e_type, epoch_info); let mut entity = Entity::create_rng(e_type, &entity_args.rng)?; - helper_functions::update_stats(entity_args, &mut entity); + helper_functions::update_stats(&entity_args.stats, false, &mut entity); // Specifying a name means the name needs to be changed // Specifying an id however means nothing and should be ignored @@ -348,7 +386,7 @@ fn handle_show_entity( /// Function handler for the edit subcommand fn handle_edit_entity( - entity_args: &EntityArgs, + entity_args: &EditArgs, e_type: &EntityType, main_epoch: &mut EpochConfig, ) -> Result<(), EpochError> { @@ -356,10 +394,12 @@ fn handle_edit_entity( let vec_entities = helper_functions::vec_entities_mut(e_type, epoch_info); let entity = vec_entities .iter_mut() - .find(|entity| helper_functions::find_by_args(entity, &entity_args.identifier)) + .find(|entity| { + helper_functions::find_by_args(entity, &Some(entity_args.identifier.clone())) + }) .ok_or(EpochError::EntityNotFound( e_type.clone(), - entity_args.identifier.clone(), + Some(entity_args.identifier.clone()), ))?; for stat in &entity_args.stats { @@ -368,12 +408,9 @@ fn handle_edit_entity( if let Some(duration) = &entity_args.duration { entity.modify_duration(duration.clone()); } - // // Only update the name if an id was given - // if entity_args.id.is_some() - // && let Some(name) = &entity_args.name - // { - // entity.name = name.clone(); - // } + if let Some(ref name) = entity_args.new_name { + entity.name = name.clone(); + } if let Some(effect) = &entity_args.effect { entity.modify_effect_application(effect.clone()); } diff --git a/bin_efo/src/commands/entity_generic/helper_functions.rs b/bin_efo/src/commands/entity_generic/helper_functions.rs index fa135d5..dd20b44 100644 --- a/bin_efo/src/commands/entity_generic/helper_functions.rs +++ b/bin_efo/src/commands/entity_generic/helper_functions.rs @@ -4,20 +4,19 @@ use lib_efo::{ entity::{EffectBounds, Entity, EntityIdentifier}, error::EpochError, naming::NAMING, - stats::EntityType, + stats::{EntityType, StatPair}, }; -use super::EntityArgs; - +use crate::commands::entity_generic::AddArgs; /// Update the given stats with the given argumenbts /// Note that this function will not change name & id due to the usage variance between caller -pub(crate) fn update_stats(entity_args: &EntityArgs, e: &mut Entity) { - if entity_args.delta { - for stat in &entity_args.stats { +pub(crate) fn update_stats(pair_list: &Vec, is_a_delta: bool, e: &mut Entity) { + if is_a_delta { + for stat in pair_list { e.delta_stat(stat.clone()); } } else { - for stat in &entity_args.stats { + for stat in pair_list { e.change_stat(stat.clone()); } } @@ -134,7 +133,7 @@ pub(crate) fn find_by_args( // } /// Update the default values of the entity with sensible defaults -pub(crate) fn update_defaults(entity_args: &EntityArgs, e: &mut Entity) { +pub(crate) fn update_defaults(entity_args: &AddArgs, e: &mut Entity) { let entity_type = &e.entity_type; let (duration, effect, bounds, name) = match entity_type { @@ -182,7 +181,7 @@ pub(crate) fn update_defaults(entity_args: &EntityArgs, e: &mut Entity) { e.modify_duration(duration); } - if let Some(EntityIdentifier::Name(name)) = &entity_args.identifier { + if let Some(name) = &entity_args.identifier { e.name = name.clone() } else { e.name = name