Running the application appears to require the command to be given twice #2

Closed
opened 2026-04-02 12:39:49 +02:00 by jakob · 2 comments
Owner

To actually perform an action the command has to be given twice like this:
./invoice-generator-v2 generate generate input.txt output

Only including the command once results in:
Usage: generate generate <csv-file> <output-dir> [--env <name>]

To actually perform an action the command has to be given twice like this: `./invoice-generator-v2 generate generate input.txt output` Only including the command once results in: `Usage: generate generate <csv-file> <output-dir> [--env <name>]`
jakob added the bug label 2026-04-02 12:39:49 +02:00
Collaborator

Root Cause Analysis

The bug is in src/main.rs:264-276 in the remove_env_flags function.

How it works:

  • parse_env_flag (lines 245-258) looks for --env and returns (Env, index)
  • If no --env flag exists, it returns (Env::default(), 0) - the index 0

The bug (line 269):

if i == env_idx || (i == env_idx + 1 && args.get(env_idx) == Some(&"--env".to_string()))

When there is NO --env flag:

  • parse_env_flag returns index 0
  • This incorrectly skips args[0] (the program name!)
  • Then args[1] ("generate") becomes the new args[0]
  • The CLI requires a second "generate" to work correctly

Suggested fix: Check if --env was actually found before removing anything:

fn remove_env_flags(args: &[String]) -> Vec<String> {
    let (env, env_idx) = parse_env_flag(args);
    // Only remove flags if --env was actually found (env_idx > 0)
    if env_idx == 0 { return args.to_vec(); }
    // ... rest of function
}
## Root Cause Analysis The bug is in `src/main.rs:264-276` in the `remove_env_flags` function. **How it works:** - `parse_env_flag` (lines 245-258) looks for `--env` and returns `(Env, index)` - If no `--env` flag exists, it returns `(Env::default(), 0)` - the index 0 **The bug (line 269):** ```rust if i == env_idx || (i == env_idx + 1 && args.get(env_idx) == Some(&"--env".to_string())) ``` When there is NO `--env` flag: - `parse_env_flag` returns index 0 - This incorrectly skips `args[0]` (the program name!) - Then `args[1]` ("generate") becomes the new `args[0]` - The CLI requires a second "generate" to work correctly **Suggested fix:** Check if `--env` was actually found before removing anything: ```rust fn remove_env_flags(args: &[String]) -> Vec<String> { let (env, env_idx) = parse_env_flag(args); // Only remove flags if --env was actually found (env_idx > 0) if env_idx == 0 { return args.to_vec(); } // ... rest of function } ```
Collaborator

Fixed in PR #4 - Commit: 460bb46

The issue was in remove_env_flags() at src/main.rs:264 which returned index 0 when no --env flag was present, incorrectly skipping the first argument (program name). Added early return when env_idx == 0 to preserve args when no --env flag exists.

Fixed in PR #4 - Commit: 460bb46 The issue was in `remove_env_flags()` at src/main.rs:264 which returned index 0 when no `--env` flag was present, incorrectly skipping the first argument (program name). Added early return when `env_idx == 0` to preserve args when no --env flag exists.
jakob closed this issue 2026-04-10 14:18:20 +02:00
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jakob/rusty-petroleum#2