Skip to content

Commit

Permalink
Merge pull request #22 from samgozman/19-add-option-to-pass-only-one-…
Browse files Browse the repository at this point in the history
…param-all

Add --one-param option to pass single param for all resources
  • Loading branch information
samgozman authored Apr 12, 2023
2 parents fb5f0a9 + c59d0d3 commit 9c4b886
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ In complex mode, you can create configuration files for each site that you want
🔺 // Link to the example config file

```bash
rvp batch --path ./stock-info.toml --params AAPL --json
rvp batch --path ./stock-info.toml --one-param AAPL --json
```

> `--params` option can be specified for each site in the config file. It simply replaces the `%%` placeholder in the URL. If you have multiple resources to parse, you can specify them as a space-separated list.
> `--one-param` option can be specified for each site in the config file. It simply replaces the `%%` placeholder in the URL. With this option, you can specify a **single parameter** that will be passed for all resources with the `%%` placeholder in the URL.
Or for example, you can run the following command to parse the weather forecast for multiple cities:

🔺 // Link to the example config file

```bash
rvp batch --path ./weather.toml --params "new-york/new-york" "california/los-angeles" "illinois/chicago" --json
```

> `--params` option can be specified for each site in the config file. It simply replaces the `%%` placeholder in the URL. If you have **multiple resources** to parse, you can specify them as a **space-separated list**.
RVP's batch mode allows you to retrieve information from multiple sources and multiple values at once, making it a powerful tool for web scraping and data extraction.

Expand Down
39 changes: 31 additions & 8 deletions src/commands/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ pub struct Args {
#[arg(long, num_args(0..))]
params: Option<Vec<String>>,

/// (Optional) Single parameter to be passed to all resources.
/// This argument is mutually exclusive with `params`.
///
/// Example:
///
/// ```
/// --one-param param1
/// ```
///
/// This argument is useful when you want to pass the same parameter to all resources.
#[arg(long, conflicts_with = "params")]
one_param: Option<String>,

/// Output the data in JSON format
#[arg(long)]
json: bool,
Expand All @@ -54,22 +67,32 @@ pub async fn command(args: Args) -> Result<()> {
let mut config = Config::from_file(&args.path, &config_format)?;

if config.needs_parameters() {
if args.params.is_none() {
if args.params.is_none() && args.one_param.is_none() {
return Err(anyhow!(
"This config needs parameters!\nMore info: rvp batch --help"
));
}

let params = args.params.unwrap();
if args.params.is_some() {
let params = args.params.unwrap();

if config.resources.len() != params.len() {
return Err(anyhow!(
"The number of parameters does not match the number of resources!"
));
if config.resources.len() != params.len() {
return Err(anyhow!(
"The number of parameters does not match the number of resources!"
));
}

for (i, param) in params.iter().enumerate() {
config.resources[i].mut_url_with_param(param);
}
}

for (i, param) in params.iter().enumerate() {
config.resources[i].mut_url_with_param(param);
if args.one_param.is_some() {
let param = args.one_param.unwrap();

for resource in config.resources.iter_mut() {
resource.mut_url_with_param(&param);
}
}
}

Expand Down

0 comments on commit 9c4b886

Please sign in to comment.