Skip to content

Commit

Permalink
Merge pull request #2307 from jqnatividad/replace-not-one-flag
Browse files Browse the repository at this point in the history
`replace`: add `--not-one` flag (resolves #2305)
  • Loading branch information
jqnatividad authored Nov 23, 2024
2 parents 5a0fb3e + fcba7b5 commit 6a88d20
Show file tree
Hide file tree
Showing 10 changed files with 1,204 additions and 1,174 deletions.
2 changes: 1 addition & 1 deletion contrib/completions/examples/qsv.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3440,7 +3440,7 @@ _qsv() {
return 0
;;
qsv__replace)
opts="-h --ignore-case --literal --select --unicode --size-limit --dfa-size-limit --output --no-headers --delimiter --progressbar --quiet --help"
opts="-h --ignore-case --literal --select --unicode --size-limit --dfa-size-limit --not-one --output --no-headers --delimiter --progressbar --quiet --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down
1 change: 1 addition & 0 deletions contrib/completions/examples/qsv.elv
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,7 @@ set edit:completion:arg-completer[qsv] = {|@words|
cand --unicode 'unicode'
cand --size-limit 'size-limit'
cand --dfa-size-limit 'dfa-size-limit'
cand --not-one 'not-one'
cand --output 'output'
cand --no-headers 'no-headers'
cand --delimiter 'delimiter'
Expand Down
3 changes: 3 additions & 0 deletions contrib/completions/examples/qsv.fig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,9 @@ const completion: Fig.Spec = {
{
name: "--dfa-size-limit",
},
{
name: "--not-one",
},
{
name: "--output",
},
Expand Down
1 change: 1 addition & 0 deletions contrib/completions/examples/qsv.fish
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ complete -c qsv -n "__fish_qsv_using_subcommand replace" -l select
complete -c qsv -n "__fish_qsv_using_subcommand replace" -l unicode
complete -c qsv -n "__fish_qsv_using_subcommand replace" -l size-limit
complete -c qsv -n "__fish_qsv_using_subcommand replace" -l dfa-size-limit
complete -c qsv -n "__fish_qsv_using_subcommand replace" -l not-one
complete -c qsv -n "__fish_qsv_using_subcommand replace" -l output
complete -c qsv -n "__fish_qsv_using_subcommand replace" -l no-headers
complete -c qsv -n "__fish_qsv_using_subcommand replace" -l delimiter
Expand Down
1 change: 1 addition & 0 deletions contrib/completions/examples/qsv.nu
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ module completions {
--unicode
--size-limit
--dfa-size-limit
--not-one
--output
--no-headers
--delimiter
Expand Down
2,341 changes: 1,171 additions & 1,170 deletions contrib/completions/examples/qsv.ps1

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contrib/completions/examples/qsv.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ _arguments "${_arguments_options[@]}" : \
'--unicode[]' \
'--size-limit[]' \
'--dfa-size-limit[]' \
'--not-one[]' \
'--output[]' \
'--no-headers[]' \
'--delimiter[]' \
Expand Down
1 change: 1 addition & 0 deletions contrib/completions/src/cmd/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn replace_cmd() -> Command {
arg!(--unicode),
arg!(--"size-limit"),
arg!(--"dfa-size-limit"),
arg!(--"not-one"),
arg!(--output),
arg!(--"no-headers"),
arg!(--delimiter),
Expand Down
8 changes: 5 additions & 3 deletions src/cmd/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ backslash or by wrapping the replacement string into single quotes:
$ qsv replace "hel(lo)" "hal\$1" file.csv
Returns exitcode 0 when replacements are done, returning number of replacements to stderr.
Returns exitcode 1 when no replacements are done.
Returns exitcode 1 when no replacements are done, unless the '--not-one' flag is used.
For more examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_replace.rs.
Expand All @@ -36,12 +36,13 @@ replace options:
will match all unicode word characters instead of only
ASCII word characters. Decreases performance.
--size-limit <mb> Set the approximate size limit (MB) of the compiled
regular expression. If the compiled expression exceeds this
regular expression. If the compiled expression exceeds this
number, then a compilation error is returned.
[default: 50]
--dfa-size-limit <mb> Set the approximate size of the cache (MB) used by the regular
expression engine's Discrete Finite Automata.
[default: 10]
--not-one Use exit code 0 instead of 1 for no replacement found.
Common options:
-h, --help Display this message
Expand Down Expand Up @@ -84,6 +85,7 @@ struct Args {
flag_literal: bool,
flag_size_limit: usize,
flag_dfa_size_limit: usize,
flag_not_one: bool,
flag_progressbar: bool,
flag_quiet: bool,
}
Expand Down Expand Up @@ -211,7 +213,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if !args.flag_quiet {
eprintln!("{total_match_ctr}");
}
if total_match_ctr == 0 {
if total_match_ctr == 0 && !args.flag_not_one {
return Err(CliError::NoMatch());
}

Expand Down
19 changes: 19 additions & 0 deletions tests/test_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ fn replace_nomatch() {
wrk.assert_err(&mut cmd);
}

#[test]
fn replace_nomatch_notone() {
let wrk = Workdir::new("replace_nomatch_notone");
wrk.create(
"data.csv",
vec![
svec!["identifier", "color"],
svec!["164.5", "yellow"],
svec!["165.6", "yellow"],
svec!["166.7", "yellow"],
svec!["167.8", "yellow.1"],
],
);
let mut cmd = wrk.command("replace");
cmd.arg("\\.0$").arg("").arg("data.csv").arg("--not-one");

wrk.assert_success(&mut cmd);
}

#[test]
fn replace_null() {
let wrk = Workdir::new("replace_null");
Expand Down

0 comments on commit 6a88d20

Please sign in to comment.