Skip to content

Commit

Permalink
refactor: Switch to Clap v4 (#122)
Browse files Browse the repository at this point in the history
* refactor: Switch to Clap v4

* chore: Changelog update
  • Loading branch information
evensolberg authored Mar 24, 2023
1 parent 0013792 commit 32d7f0f
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 172 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ All notable changes to this project will be documented in this file.

- More commonalities and simplification
- Remove TimeStamp (#113)
- Lint fixes
- Clean up lints
- Remove lint
- Lint fix (#116)
- Switch to Clap v4

### Build

Expand Down
77 changes: 32 additions & 45 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions fit2csv/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "fit2csv"
version = "0.4.1"
version = "0.5.0"
description = "Parses .FIT files to .JSON and .CSV"
license = "Apache-2.0"
authors = ["evensolberg <[email protected]>"]
edition = "2021"
include = ["src/**/*", "README.md"]

[dependencies]
clap = { version = "3.1.12", features = ["cargo", "color"] }
clap = { version = "4.1.9", features = ["cargo", "wrap_help", "env"] }
env_logger = "0.10.0"
log = "0.4.16"

Expand Down
28 changes: 15 additions & 13 deletions fit2csv/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Contains a single function to build the CLI
use clap::{Arg, ArgMatches, Command};
use clap::{Arg, ArgAction, ArgMatches, Command};

/// Builds the CLI so the main file doesn't get cluttered.
pub fn build() -> ArgMatches {
Expand All @@ -12,52 +12,54 @@ pub fn build() -> ArgMatches {
Arg::new("read")
.value_name("FILE(S)")
.help("One or more .fit file(s) to process. Wildcards and multiple_occurrences files (e.g. 2019*.fit 2020*.fit) are supported.")
.takes_value(true)
.num_args(1..)
.required(true)
.multiple_occurrences(true),
.action(ArgAction::Append)
)
.arg( // Hidden debug parameter
Arg::new("debug")
.short('d')
.long("debug")
.multiple_occurrences(true)
.help("Output debug information as we go. Supply it twice for trace-level logs.")
.takes_value(false)
.env("FIT_DEBUG")
.num_args(0)
.action(ArgAction::Count)
.hide(true),
)
.arg( // Don't print any information
Arg::new("quiet")
.short('q')
.long("quiet")
.multiple_occurrences(false)
.help("Don't produce any output except errors while working.")
.takes_value(false)
.num_args(0)
.action(ArgAction::SetTrue)
)
.arg( // Print summary information
Arg::new("print-summary")
.short('p')
.long("print-summary")
.multiple_occurrences(false)
.help("Print summary detail for each session processed.")
.takes_value(false)
.num_args(0)
.action(ArgAction::SetTrue)
)
.arg( // Don't export detail information
Arg::new("detail-off")
.short('o')
.long("detail-off")
.multiple_occurrences(false)
.help("Don't export detailed information from each file parsed.")
.takes_value(false)
.requires("summary-file")
.num_args(0)
.action(ArgAction::SetTrue)
)
.arg( // Summary file name
Arg::new("summary-file")
.short('s')
.value_name("summary output file name")
.long("summary-file")
.multiple_occurrences(false)
.help("Summary output file name.")
.takes_value(true)
.num_args(1)
.required(false)
.action(ArgAction::Set)
)
.get_matches()
}
32 changes: 22 additions & 10 deletions fit2csv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use env_logger::Target;
use std::error::Error;
use utilities::{FITActivities, FITActivity};

use clap::parser::ValueSource;

mod cli;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -15,19 +17,25 @@ fn run() -> Result<(), Box<dyn Error>> {
logbuilder.target(Target::Stdout).init();

// If tracing, output the names of the files being processed
for argument in cli_args.values_of("read").unwrap_or_default() {
for argument in cli_args
.get_many::<String>("read")
.unwrap_or_default()
.map(std::string::String::as_str)
{
log::trace!("main::run() -- Arguments: {argument:?}");
}

// Find the name of the session output file
let session_file_name = String::from("fit-sessions.csv");
let sessionfile = cli_args
.value_of("summary-file")
.unwrap_or("fit-sessions.csv");
.get_one::<String>("summary-file")
.unwrap_or(&session_file_name)
.as_str();
log::debug!("main::run() -- session output file: {sessionfile}");

// Let the user know if we're writing
if cli_args.is_present("detail-off") {
log::info!("Writing summary file {} only.", &sessionfile);
if cli_args.value_source("detail-off") == Some(ValueSource::CommandLine) {
log::info!("Writing summary file {sessionfile} only.");
} else {
log::info!("Writing detail files.");
}
Expand All @@ -38,19 +46,23 @@ fn run() -> Result<(), Box<dyn Error>> {
// Create an empty placeholder for all the activities
let mut activities = FITActivities::new();

for filename in cli_args.values_of("read").unwrap_or_default() {
for filename in cli_args
.get_many::<String>("read")
.unwrap_or_default()
.map(std::string::String::as_str)
{
log::info!("Processing file: {filename}");

// Parse the FIT file
let activity = FITActivity::from_file(filename)?;

// Output the files
if cli_args.is_present("print-summary") {
if cli_args.value_source("print-summary") == Some(ValueSource::CommandLine) {
activity.session.print_summary();
}

// Export the data if requested
if !cli_args.is_present("detail-off") {
if cli_args.value_source("detail-off") != Some(ValueSource::CommandLine) {
activity.export()?;
}

Expand All @@ -59,8 +71,8 @@ fn run() -> Result<(), Box<dyn Error>> {
}

// Export the summary information
if cli_args.is_present("summary-file") {
log::info!("Summary information written to: {}", &sessionfile);
if cli_args.value_source("summary-file") == Some(ValueSource::CommandLine) {
log::info!("Summary information written to: {sessionfile}");
activities.export_summary_csv(sessionfile)?;
}

Expand Down
4 changes: 2 additions & 2 deletions fitrename/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "fitrename"
version = "0.3.1"
version = "0.4.0"
edition = "2021"
description = "Renames .FIT, .GPX and .TCX files based on metadata."
license = "Apache-2.0"
Expand All @@ -12,7 +12,7 @@ include = ["src/**/*", "README.md"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "3.1.12", features = ["cargo", "color"] }
clap = { version = "4.1.9", features = ["cargo", "wrap_help", "env"] }
env_logger = "0.10.0"
log = "0.4.16"

Expand Down
Loading

0 comments on commit 32d7f0f

Please sign in to comment.