diff --git a/Changes b/Changes index 620dbe7..ed67176 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,17 @@ Revision history for App-Rak {{$NEXT}} +0.0.12 2022-07-16T22:17:25+02:00 + - Process named arg substitutions in the order they are specified + - Remove all aliases for each option: each option can only be accessed + by default using a single (long) name, for clarity. Users can add + their own shortcuts with --save, or add a set from someone adhering + to either "grep", "ack" or "ag" options + - Changed wording in documentation to refer to "options" if they are + about optional named arguments on the command line + - Change "list-tags" option to "list-additional-options", to be more + in line with names used + 0.0.11 2022-07-16T13:29:57+02:00 - Remove "--with" named argument: you can now use any saved named argument directly, without having to use --with diff --git a/META6.json b/META6.json index bdc1f70..e74b904 100644 --- a/META6.json +++ b/META6.json @@ -31,5 +31,5 @@ ], "test-depends": [ ], - "version": "0.0.11" + "version": "0.0.12" } diff --git a/README.md b/README.md index 80a2d29..5e23d53 100644 --- a/README.md +++ b/README.md @@ -37,32 +37,32 @@ pattern The pattern to search for. This can either be a string, or a regular expression (indicated by a string starting and ending with **/**), or a Callable (indicated by a string starting with **{** and ending with **}**. -Can also be specified with the `--pattern` named argument, in which case all the positional arguments are considered to be a path specification. +Can also be specified with the `--pattern` option, in which case **all** the positional arguments are considered to be a path specification. path(s) ------- -Optional. Either indicates the path of the directory (and its sub-directories), or the file that will be searched. By default, all directories that do not start with a period, will be recursed into (but this can be changed with the `--dir` named argument). +Optional. Either indicates the path of the directory (and its sub-directories), or the file that will be searched. By default, all directories that do not start with a period, will be recursed into (but this can be changed with the `--dir` option). -By default, all files will be searched in the directories. This can be changed with the `--file` named argument. +By default, all files will be searched in the directories. This can be changed with the `--file` option -SUPPORTED NAMED ARGUMENTS -========================= +SUPPORTED OPTIONS +================= -All named arguments are optional. Any unexpected named arguments, will cause an exception with the unexpected named arguments listed. +All options are optional. Any unexpected options, will cause an exception to be thrown with the unexpected options listed. --A --after --after-context --------------------------- +--after-context +--------------- Indicate the number of lines that should be shown **after** any line that matches. Defaults to **0**. Will be overridden by a `-C` or `--context` argument. --B --before --before-context ----------------------------- +--before-context +---------------- Indicate the number of lines that should be shown **before** any line that matches. Defaults to **0**. Will be overridden by a `-C` or `--context` argument. --C --context ------------- +--context +--------- Indicate the number of lines that should be shown **around** any line that matches. Defaults to **0**. Overrides any a `-A`, `--after`, `--after-context`, `-B`, `--before` or `--before-context` argument. argument. @@ -71,8 +71,8 @@ Indicate the number of lines that should be shown **around** any line that match Indicate whether the patterns found should be fed into an editor for inspection and/or changes. Defaults to `False`. --h --no-filename ----------------- +--no-filename +------------- Indicate whether filenames should **not** be shown. Defaults to `False` if `--human` is (implicitely) set to `True`, else defaults to `True`. @@ -96,29 +96,29 @@ Indicate the string that should be used at the end of the pattern found in a lin Indicate that search results should be presented in a human readable manner. This means: filenames shown on a separate line, line numbers shown, and highlighting performed. Defaults to `True` if `STDOUT` is a TTY (aka, someone is actually watching the search results), otherwise defaults to `False`. --l --files-only --files-with-matches ------------------------------------- +--files-with-matches +-------------------- If specified with a true value, will only produce the filenames of the files in which the pattern was found. Defaults to `False`. ---list-tags ------------ +--list-additional-options +------------------------- ```bash -$ rak --list-tags +$ rak --list-additional-options fs: --'follow-symlinks' im: --ignorecase --ignoremark ``` -If specified with a true value and as the only named argument, will list all saved tags. +If specified with a true value and as the only option, will list all additional options previously saved with `--save`. --n --line-number ----------------- +--line-number +------------- Indicate whether line numbers should be shown. Defaults to `True` if `--human` is (implicitely) set to `True` and <-h> is **not** set to `True`, else defaults to `False`. --o --only-matching ------------------- +--only-matching +--------------- Indicate whether only the matched pattern should be produced, rather than the line in which the pattern was found. Defaults to `False`. @@ -140,19 +140,19 @@ Only makes sense if the specified pattern is a `Callable`. Indicates whether the --save ------ -Save all named arguments with the given tag in the configuration file (`~/.rak-config.json`), and exit with a message that the named arguments have been saved with the given tag. +Save all named arguments with the given name in the configuration file (`~/.rak-config.json`), and exit with a message that these options have been saved with the given name. This feature can used to both create shortcuts for specific (long) named arguments, or just as a convenient way to combine often used named arguments. ```bash $ rak --ignorecase --ignoremark --save=im -Saved configuration for 'im' +Saved option '--im' as: --ignorecase --ignoremark # same as --ignorecase --ignoremark $ rak foo --im $ rak --follow-symlinks --save=fs -Saved configuration for 'fs' +Saved option '--fs' as: --follow-symlinks $ rak --save=foo Removed configuration for 'foo' @@ -162,13 +162,13 @@ Any saved named arguments can be accessed as if it is a standard named boolean a To remove a saved set of named arguments, use `--save` as the only named argument. ---sum --summary-if-larger-than ------------------------------- +--summary-if-larger-than +------------------------ Indicate the maximum size a line may have before it will be summarized. Defaults to `160` if `STDOUT` is a TTY (aka, someone is actually watching the search results), otherwise defaults to `Inf` effectively (indicating no summarization will ever occur). --S --follow-symlinks --------------------- +--follow-symlinks +----------------- Indicate whether symbolic links to directories should be followed. Defaults to `False`. @@ -177,19 +177,19 @@ Indicate whether symbolic links to directories should be followed. Defaults to ` Indicate whether lines that have the pattern, should have any whitespace at the start and/or end of the line removed. Defaults to `True` if no context for lines was specified, else defaults to `False`. --V --version ------------- +--version +--------- If the only argument, shows the name and version of the script, and the system it is running on. -CREATING YOUR OWN NAMED ARGUMENTS -================================= +CREATING YOUR OPTIONS +===================== -You can use the `--save` named argument to save a set of named arguments and than later access it with the given name: +You can use the `--save` option to save a set of options and than later access them with the given name: ```bash $ rak --ignorecase --ignoremark --save=im -Saved configuration for 'im' +Saved option '--im' as: --ignorecase --ignoremark # same as --ignorecase --ignoremark $ rak foo --im diff --git a/lib/App/Rak.rakumod b/lib/App/Rak.rakumod index df35069..b1c95b9 100644 --- a/lib/App/Rak.rakumod +++ b/lib/App/Rak.rakumod @@ -32,10 +32,25 @@ my sub is-simple-Callable($needle) { Callable.ACCEPTS($needle) && !Regex.ACCEPTS($needle) } -# Process all alternate names / values into a single value and remove it -my sub named-arg(%_, *@names) { - return %_.DELETE-KEY($_) if %_.EXISTS-KEY($_) for @names; - Nil +# Return string before marker, or string if no marker +my sub before(Str:D $string, Str:D $marker) { + with $string.index($marker) { + $string.substr(0,$_) + } + else { + $string + } +} + +# Return named variables in order of specification on the command line +my sub original-nameds() { + @*ARGS.map: { + .starts-with("--") + ?? before(.substr(2), "=") + !! .starts-with("-") + ?? before(.substr(1), "=") + !! Empty + } } # Process all alternate names / values into a Map and remove them @@ -95,15 +110,17 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues my %config := from-json($config-file.slurp) if $config-file.e; # Saving config - if %n:delete -> $tag { - %n ?? (%config{$tag} := %n) !! (%config{$tag}:delete); + if %n:delete -> $option { + %n ?? (%config{$option} := %n) !! (%config{$option}:delete); $config-file.spurt: to-json %config, :!pretty, :sorted-keys; - say (%n ?? "Saved" !! "Removed") ~ " configuration for '--$tag'"; + say %n + ?? "Saved option '--$option' as: " ~ as-cli-arguments(%n) + !! "Removed option '--$option'"; exit; } # Show what we have - elsif %n:delete { + elsif %n:delete { meh-if-unexpected(%n); my $format := '%' ~ %config.keys>>.chars.max ~ 's: '; @@ -114,10 +131,11 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues # Translate any custom parameters my @strange; - for %n.sort(*.key.fc) -> (:key($tag), :$value) { + for original-nameds() -> $option { + my $value := %n{$option}; if Bool.ACCEPTS($value) { - if %config{$tag} -> %adding { - %n{$tag}:delete; + if %config{$option} -> %adding { + %n{$option}:delete; if $value { %n{.key} = .value unless %n{.key}:exists for %adding; } @@ -127,10 +145,10 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues } } else { - @strange.push: "--$tag"; + @strange.push: "--$option"; } } - meh "Must be flags, did you mean: @strange[] ?" if @strange; + meh "These options Must be flags, did you mean: @strange[] ?" if @strange; my $needle = %n:delete // @specs.shift; meh "Must at least specify a pattern" without $needle; @@ -158,7 +176,7 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues } @specs.unshift(".") without $root; - my %additional := named-args %n, :follow-symlinks, :file :dir; + my %additional := named-args %n, :follow-symlinks, :file :dir; my @paths = (@specs == 1 ?? paths(@specs.head, |%additional) !! @specs.&hyperize(1, %n).map({ paths($_, |%additional).Slip }) @@ -168,13 +186,13 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues ?? go-edit-files($needle, @paths, %n) !! is-simple-Callable($needle) && (%n:delete) ?? replace-files($needle, @paths, %n) - !! named-arg(%n, ) + !! (%n:delete) ?? files-only($needle, @paths, %n) !! want-lines($needle, @paths, %n); } my sub go-edit-files($needle, @paths, %_ --> Nil) { - my $files-only := named-arg %_, ; + my $files-only := %_:delete; my %ignore := named-args %_, :ignorecase, :ignoremark, @@ -212,16 +230,16 @@ my sub files-only($needle, @paths, %_ --> Nil) { } my sub want-lines($needle, @paths, %_ --> Nil) { - my $ignorecase := named-arg %_, ; - my $ignoremark := named-arg %_, ; + my $ignorecase := %_:delete; + my $ignoremark := %_:delete; my $seq := files-containing $needle, @paths, :$ignorecase, :$ignoremark, :offset(1), - |named-args %_, :invert-match, :max-count, :batch, :degree, + |named-args %_, :invert-match, :max-count, :batch, :degree, ; - my UInt() $before = $_ with named-arg %_, ; - my UInt() $after = $_ with named-arg %_, ; - $before = $after = $_ with named-arg %_, ; + my UInt() $before = $_ with %_:delete; + my UInt() $after = $_ with %_:delete; + $before = $after = $_ with %_:delete; $before = 0 without $before; $after = 0 without $after; @@ -240,17 +258,16 @@ my sub want-lines($needle, @paths, %_ --> Nil) { $summary-if-larger-than = 160; } - $highlight = $_ with named-arg %_, ; - $trim = $_ with named-arg %_, ; - $only = $_ with named-arg %_, ; + $highlight = $_ with %_:delete; + $trim = $_ with %_:delete; + $only = $_ with %_:delete; $before = $after = 0 if $only; - $summary-if-larger-than = $_ - with named-arg %_, ; + $summary-if-larger-than = $_ with %_:delete; my &show-line; if $highlight { - my Str() $pre = my Str() $post = named-arg(%_, ); - $post = $_ with named-arg %_, ; + my Str() $pre = my Str() $post = $_ with %_:delete; + $post = $_ with %_:delete; $pre = $only ?? " " !! BON without $pre; $post = $only ?? "" !! BOFF without $post; @@ -277,9 +294,9 @@ my sub want-lines($needle, @paths, %_ --> Nil) { } # some twisted historical logic - $no-filename = $_ with named-arg %_, ; + $no-filename = $_ with %_:delete; $no-filename = True without $no-filename; - $line-number = $_ with named-arg %_, ; + $line-number = $_ with %_:delete; without $line-number { $line-number = !$no-filename if $human; } @@ -358,37 +375,37 @@ The pattern to search for. This can either be a string, or a regular expression (indicated by a string starting and ending with B), or a Callable (indicated by a string starting with B<{> and ending with B<}>. -Can also be specified with the C<--pattern> named argument, in which -case all the positional arguments are considered to be a path specification. +Can also be specified with the C<--pattern> option, in which case B +the positional arguments are considered to be a path specification. =head2 path(s) Optional. Either indicates the path of the directory (and its sub-directories), or the file that will be searched. By default, all directories that do not start with a period, will be recursed into (but -this can be changed with the C<--dir> named argument). +this can be changed with the C<--dir> option). By default, all files will be searched in the directories. This can be -changed with the C<--file> named argument. +changed with the C<--file> option -=head1 SUPPORTED NAMED ARGUMENTS +=head1 SUPPORTED OPTIONS -All named arguments are optional. Any unexpected named arguments, will -cause an exception with the unexpected named arguments listed. +All options are optional. Any unexpected options, will cause an exception +to be thrown with the unexpected options listed. -=head2 -A --after --after-context +=head2 --after-context Indicate the number of lines that should be shown B any line that matches. Defaults to B<0>. Will be overridden by a C<-C> or C<--context> argument. -=head2 -B --before --before-context +=head2 --before-context Indicate the number of lines that should be shown B any line that matches. Defaults to B<0>. Will be overridden by a C<-C> or C<--context> argument. -=head2 -C --context +=head2 --context Indicate the number of lines that should be shown B any line that matches. Defaults to B<0>. Overrides any a C<-A>, C<--after>, @@ -400,7 +417,7 @@ argument. Indicate whether the patterns found should be fed into an editor for inspection and/or changes. Defaults to C. -=head2 -h --no-filename +=head2 --no-filename Indicate whether filenames should B be shown. Defaults to C if C<--human> is (implicitely) set to C, else defaults to C. @@ -433,31 +450,31 @@ shown, and highlighting performed. Defaults to C if C is a TTY (aka, someone is actually watching the search results), otherwise defaults to C. -=head2 -l --files-only --files-with-matches +=head2 --files-with-matches If specified with a true value, will only produce the filenames of the files in which the pattern was found. Defaults to C. -=head2 --list-tags +=head2 --list-additional-options =begin code :lang -$ rak --list-tags +$ rak --list-additional-options fs: --'follow-symlinks' im: --ignorecase --ignoremark =end code -If specified with a true value and as the only named argument, will list -all saved tags. +If specified with a true value and as the only option, will list all +additional options previously saved with C<--save>. -=head2 -n --line-number +=head2 --line-number Indicate whether line numbers should be shown. Defaults to C if C<--human> is (implicitely) set to C and <-h> is B set to C, else defaults to C. -=head2 -o --only-matching +=head2 --only-matching Indicate whether only the matched pattern should be produced, rather than the line in which the pattern was found. Defaults to C. @@ -482,9 +499,9 @@ it was found. Defaults to C. =head2 --save -Save all named arguments with the given tag in the configuration file -(C<~/.rak-config.json>), and exit with a message that the named arguments -have been saved with the given tag. +Save all named arguments with the given name in the configuration file +(C<~/.rak-config.json>), and exit with a message that these options have +been saved with the given name. This feature can used to both create shortcuts for specific (long) named arguments, or just as a convenient way to combine often used named arguments. @@ -492,13 +509,13 @@ arguments, or just as a convenient way to combine often used named arguments. =begin code :lang $ rak --ignorecase --ignoremark --save=im -Saved configuration for 'im' +Saved option '--im' as: --ignorecase --ignoremark # same as --ignorecase --ignoremark $ rak foo --im $ rak --follow-symlinks --save=fs -Saved configuration for 'fs' +Saved option '--fs' as: --follow-symlinks $ rak --save=foo Removed configuration for 'foo' @@ -513,14 +530,14 @@ depend on other arguments having been specified. To remove a saved set of named arguments, use C<--save> as the only named argument. -=head2 --sum --summary-if-larger-than +=head2 --summary-if-larger-than Indicate the maximum size a line may have before it will be summarized. Defaults to C<160> if C is a TTY (aka, someone is actually watching the search results), otherwise defaults to C effectively (indicating no summarization will ever occur). -=head2 -S --follow-symlinks +=head2 --follow-symlinks Indicate whether symbolic links to directories should be followed. Defaults to C. @@ -531,20 +548,20 @@ Indicate whether lines that have the pattern, should have any whitespace at the start and/or end of the line removed. Defaults to C if no context for lines was specified, else defaults to C. -=head2 -V --version +=head2 --version If the only argument, shows the name and version of the script, and the system it is running on. -=head1 CREATING YOUR OWN NAMED ARGUMENTS +=head1 CREATING YOUR OPTIONS -You can use the C<--save> named argument to save a set of named arguments -and than later access it with the given name: +You can use the C<--save> option to save a set of options and than later +access them with the given name: =begin code :lang $ rak --ignorecase --ignoremark --save=im -Saved configuration for 'im' +Saved option '--im' as: --ignorecase --ignoremark # same as --ignorecase --ignoremark $ rak foo --im diff --git a/run-tests b/run-tests index 0528988..a500eb2 100644 --- a/run-tests +++ b/run-tests @@ -32,9 +32,6 @@ sub test-dir($dir) { say $stdout; } } - elsif $proc.err -> $stderr { - say .slurp with $stderr; - } else { say "No output received, exit-code $proc.exitcode() ($proc.signal())"; }