What are fish shell command-line ui patterns? - fish

I want to construct an ui similar to what git have git <command> [<param1> ...]. What I came up with so far is:
function git -d "Description"
switch $argv[1]
case branch
git_branch $argv[2]
case reset
git_reset
end
end
function git_branch -d "Description for branch"
do_something $argv[1]
end
function git_reset -d "Description for reset"
do_something_else
end
It works, but there are couple problems:
1. Fish doesn't pick up available commands for auto-completion;
2. If I run git with no arguments it wouldn't print out list of command, neither it would pick up descriptions for them.
It appears to me that what I'm doing is not the "right" way to construct command-line utilities with fish. So, what is the right way?

Maybe your problems occur because you don't have a default branch if your switch statement, so you never call out to the actual git command. Try:
function git -d "Description"
switch $argv[1]
case branch
git_branch $argv[2]
case reset
git_reset
case '*'
command git $argv
end
end
To protect against the case where zero arguments are provided, I do this:
function git -d "Description"
set -q argv[1]
and switch $argv[1]
case branch
git_branch $argv[2]
return
case reset
git_reset
return
end
command git $argv
end

Related

How to remove trailing newline from command arguments in fish?

As far as I know, in fish as well as in many other command lines, the return statement is reserved for success/failure codes, and the standard way to 'return a string' is to echo the result like this:
function format-commit-message
echo e2e-new: $argv[1]
echo
echo "Jira: APP-1234"
end
However, this means that all such strings end with a newline character. I tried to remove it because that is the commit message policy:
git commit -m ( format-commit-message "new commit" | string split0 | strig sub -e -1 )
But the result is:
fatal: empty string is not a valid pathspec. please use . instead if you meant to match all paths
Could you tell me what I am doing wrong?
Use echo's -n parameter to omit the newline.

Why is fish 3.0 printing an error message on start?

I recently upgraded to fish 3.0.1 via Homebrew 2.0.1 on MacOS Mojave 10.14.2. Since the upgrade, the following error message appears every time fish starts:
contains: Unknown option '-gx'
/usr/local/Cellar/fish/3.0.1/share/fish/config.fish (line 426):
if not contains $entry $result
^
in function '__fish_macos_set_env'
called on line 228 of file /usr/local/Cellar/fish/3.0.1/share/fish/config.fish
with parameter list 'PATH /etc/paths /etc/paths.d'
from sourcing file /usr/local/Cellar/fish/3.0.1/share/fish/config.fish
called during startup
contains - test if a word is present in a list
Synopsis
contains [OPTIONS] KEY [VALUES...]
contains: Type 'help contains' for related documentation
contains: Unknown option '-e'
/usr/local/Cellar/fish/3.0.1/share/fish/config.fish (line 426):
if not contains $entry $result
^
in function '__fish_macos_set_env'
called on line 228 of file /usr/local/Cellar/fish/3.0.1/share/fish/config.fish
with parameter list 'PATH /etc/paths /etc/paths.d'
from sourcing file /usr/local/Cellar/fish/3.0.1/share/fish/config.fish
called during startup
contains - test if a word is present in a list
Synopsis
contains [OPTIONS] KEY [VALUES...]
contains: Type 'help contains' for related documentation
My first reflex was to have a look in the offending file, namely /usr/local/Cellar/fish/3.0.1/share/fish/config.fish, but this file is only 306 lines long, and therefore does not seem to contain the infamous line 426. I am copying this file below, in case it proves useful:
# Main file for fish command completions. This file contains various
# common helper functions for the command completions. All actual
# completions are located in the completions subdirectory.
#
# Set default field separators
#
set -g IFS \n\ \t
set -qg __fish_added_user_paths
or set -g __fish_added_user_paths
#
# Create the default command_not_found handler
#
function __fish_default_command_not_found_handler
printf "fish: Unknown command %s\n" (string escape -- $argv[1]) >&2
end
if status --is-interactive
# The user has seemingly explicitly launched an old fish with too-new scripts installed.
if not contains -- "string" (builtin -n)
set -g __is_launched_without_string 1
# XXX nostring - fix old fish binaries with no `string' builtin.
# When executed on fish 2.2.0, the `else' block after this would
# force on 24-bit mode due to changes to in test behavior.
# These "XXX nostring" hacks were added for 2.3.1
set_color --bold
echo "You appear to be trying to launch an old fish binary with newer scripts "
echo "installed into" (set_color --underline)"$__fish_data_dir"
set_color normal
echo -e "\nThis is an unsupported configuration.\n"
set_color yellow
echo "You may need to uninstall and reinstall fish!"
set_color normal
# Remove this code when we've made it safer to upgrade fish.
else
# Enable truecolor/24-bit support for select terminals
# Ignore Screen and emacs' ansi-term as they swallow the sequences, rendering the text white.
if not set -q STY
and not string match -q -- 'eterm*' $TERM
and begin
set -q KONSOLE_PROFILE_NAME # KDE's konsole
or string match -q -- "*:*" $ITERM_SESSION_ID # Supporting versions of iTerm2 will include a colon here
or string match -q -- "st-*" $TERM # suckless' st
or test -n "$VTE_VERSION" -a "$VTE_VERSION" -ge 3600 # Should be all gtk3-vte-based terms after version 3.6.0.0
or test "$COLORTERM" = truecolor -o "$COLORTERM" = 24bit # slang expects this
end
# Only set it if it isn't to allow override by setting to 0
set -q fish_term24bit
or set -g fish_term24bit 1
end
end
else
# Hook up the default as the principal command_not_found handler
# in case we are not interactive
function __fish_command_not_found_handler --on-event fish_command_not_found
__fish_default_command_not_found_handler $argv
end
end
#
# Set default search paths for completions and shellscript functions
# unless they already exist
#
set -g __fish_config_dir ~/.config/fish
if set -q XDG_CONFIG_HOME
set __fish_config_dir $XDG_CONFIG_HOME/fish
end
set -l userdatadir ~/.local/share
if set -q XDG_DATA_HOME
set userdatadir $XDG_DATA_HOME
end
# __fish_data_dir, __fish_sysconf_dir, __fish_help_dir, __fish_bin_dir
# are expected to have been set up by read_init from fish.cpp
# Grab extra directories (as specified by the build process, usually for
# third-party packages to ship completions &c.
set -l __extra_completionsdir
set -l __extra_functionsdir
set -l __extra_confdir
if test -f $__fish_data_dir/__fish_build_paths.fish
source $__fish_data_dir/__fish_build_paths.fish
end
# Set up function and completion paths. Make sure that the fish
# default functions/completions are included in the respective path.
if not set -q fish_function_path
set fish_function_path $__fish_config_dir/functions $__fish_sysconf_dir/functions $__extra_functionsdir $__fish_data_dir/functions
end
if not contains -- $__fish_data_dir/functions $fish_function_path
set fish_function_path $fish_function_path $__fish_data_dir/functions
end
if not set -q fish_complete_path
set fish_complete_path $__fish_config_dir/completions $__fish_sysconf_dir/completions $__extra_completionsdir $__fish_data_dir/completions $userdatadir/fish/generated_completions
end
if not contains -- $__fish_data_dir/completions $fish_complete_path
set fish_complete_path $fish_complete_path $__fish_data_dir/completions
end
# This cannot be in an autoload-file because `:.fish` is an invalid filename on windows.
function :
# no-op function for compatibility with sh, bash, and others.
# Often used to insert a comment into a chain of commands without having
# it eat up the remainder of the line, handy in Makefiles.
end
#
# This is a Solaris-specific test to modify the PATH so that
# Posix-conformant tools are used by default. It is separate from the
# other PATH code because this directory needs to be prepended, not
# appended, since it contains POSIX-compliant replacements for various
# system utilities.
#
if test -d /usr/xpg4/bin
if not contains -- /usr/xpg4/bin $PATH
set PATH /usr/xpg4/bin $PATH
end
end
# Add a handler for when fish_user_path changes, so we can apply the same changes to PATH
function __fish_reconstruct_path -d "Update PATH when fish_user_paths changes" --on-variable fish_user_paths
set -l local_path $PATH
for x in $__fish_added_user_paths
set -l idx (contains --index -- $x $local_path)
and set -e local_path[$idx]
end
set -g __fish_added_user_paths
if set -q fish_user_paths
for x in $fish_user_paths[-1..1]
if set -l idx (contains --index -- $x $local_path)
set -e local_path[$idx]
else
set -g __fish_added_user_paths $__fish_added_user_paths $x
end
set local_path $x $local_path
end
end
set -xg PATH $local_path
end
#
# Launch debugger on SIGTRAP
#
function fish_sigtrap_handler --on-signal TRAP --no-scope-shadowing --description "Signal handler for the TRAP signal. Launches a debug prompt."
breakpoint
end
#
# Whenever a prompt is displayed, make sure that interactive
# mode-specific initializations have been performed.
# This handler removes itself after it is first called.
#
function __fish_on_interactive --on-event fish_prompt
__fish_config_interactive
functions -e __fish_on_interactive
end
# Set the locale if it isn't explicitly set. Allowing the lack of locale env vars to imply the
# C/POSIX locale causes too many problems. Do this before reading the snippets because they might be
# in UTF-8 (with non-ASCII characters).
__fish_set_locale
# "." command for compatibility with old fish versions.
function . --description 'Evaluate contents of file (deprecated, see "source")' --no-scope-shadowing
if test (count $argv) -eq 0
# Uses tty directly, as isatty depends on "."
and tty 0>&0 >/dev/null
echo "source: '.' command is deprecated, and doesn't work with STDIN anymore. Did you mean 'source' or './'?" >&2
return 1
else
source $argv
end
end
# Upgrade pre-existing abbreviations from the old "key=value" to the new "key value" syntax.
# This needs to be in share/config.fish because __fish_config_interactive is called after sourcing
# config.fish, which might contain abbr calls.
if not set -q __fish_init_2_3_0
if set -q fish_user_abbreviations
set -l fab
for abbr in $fish_user_abbreviations
set fab $fab (string replace -r '^([^ =]+)=(.*)$' '$1 $2' -- $abbr)
end
set fish_user_abbreviations $fab
end
set -U __fish_init_2_3_0
end
# macOS-ism: Emulate calling path_helper.
if command -sq /usr/libexec/path_helper
# Adapt construct_path from the macOS /usr/libexec/path_helper
# executable for fish; see
# https://opensource.apple.com/source/shell_cmds/shell_cmds-203/path_helper/path_helper.c.auto.html .
function __fish_macos_set_env -d "set an environment variable like path_helper does (macOS only)"
set -l result
for path_file in $argv[2] $argv[3]/*
if test -f $path_file
while read -l entry
if not contains $entry $result
set result $result $entry
end
end <$path_file
end
end
for entry in $$argv[1]
if not contains $entry $result
set result $result $entry
end
end
set -xg $argv[1] $result
end
__fish_macos_set_env 'PATH' '/etc/paths' '/etc/paths.d'
if [ -n "$MANPATH" ]
__fish_macos_set_env 'MANPATH' '/etc/manpaths' '/etc/manpaths.d'
end
functions -e __fish_macos_set_env
end
#
# Some things should only be done for login terminals
# This used to be in etc/config.fish - keep it here to keep the semantics
#
if status --is-login
#
# Put linux consoles in unicode mode.
#
if test "$TERM" = linux
if string match -qir '\.UTF' -- $LANG
if command -sq unicode_start
unicode_start
end
end
end
end
# Invoke this here to apply the current value of fish_user_path after
# PATH is possibly set above.
__fish_reconstruct_path
# Allow %n job expansion to be used with fg/bg/wait
# `jobs` is the only one that natively supports job expansion
function __fish_expand_pid_args
for arg in $argv
if string match -qr '^%\d+$' -- $arg
# set newargv $newargv (jobs -p $arg)
jobs -p $arg
if not test $status -eq 0
return 1
end
else
printf "%s\n" $arg
end
end
end
function bg
builtin bg (__fish_expand_pid_args $argv)
end
function fg
builtin fg (__fish_expand_pid_args $argv)
end
function kill
command kill (__fish_expand_pid_args $argv)
end
function wait
builtin wait (__fish_expand_pid_args $argv)
end
function disown
builtin disown (__fish_expand_pid_args $argv)
end
# As last part of initialization, source the conf directories.
# Implement precedence (User > Admin > Extra (e.g. vendors) > Fish) by basically doing "basename".
set -l sourcelist
for file in $__fish_config_dir/conf.d/*.fish $__fish_sysconf_dir/conf.d/*.fish $__extra_confdir/*.fish
set -l basename (string replace -r '^.*/' '' -- $file)
contains -- $basename $sourcelist
and continue
set sourcelist $sourcelist $basename
# Also skip non-files or unreadable files.
# This allows one to use e.g. symlinks to /dev/null to "mask" something (like in systemd).
[ -f $file -a -r $file ]
and source $file
end
What's going on here? How can I fix this?
What happens here is that a component of your $PATH, $fish_user_paths or a line in /etc/paths looks like an option.
Most likely, this is wrong, and you should remove it.
E.g. try printf '%s\n' $fish_user_paths. If that tells you that one of the entries is "-gx", then you've set it incorrectly and need to use set -e fish_user_paths[number-of-that-entry] to correct it.
Since these are all common options to set, you've probably once done something like set fish_user_paths /something -gx, which adds a "-gx" component (set only reads options before the variable name).
This has been reported upstream at https://github.com/fish-shell/fish-shell/issues/5662, and future fish versions won't spew an error, but the existence of the offending component is most likely an error, so you still want to remove it.

Handling Perforce message in Perl when there are no new files submitted

I am trying to code a Perl subroutine that returns an array of files that has been modified and submitted to the Perforce repository from $previous_date until now. This is how the subroutine looks like:
sub p4_files {
my ($previous_date) = #_;
my $files = "//depot/project/design/...rtl.sv"
my $p4cmd = "p4 files -e $files\#$previous_date,\#now";
my #filelist = `$p4cmd`;
chomp #filelist;
return #filelist;
}
The subroutine works as expected if there are files submitted between the given dates. However, it happens that no new changes are made, and executing the p4 files command returns a message instead:
prompt% p4 files -e //depot/project/design/...rtl.sv\#25/05/2017,\#now
prompt% //depot/project/design/...rtl.sv\#25/05/2017,\#now - no revision(s) after that date.
How should I handle this in my Perl script? I would like to exit the script when such a situation is encountered.
Unfortunately, p4 returns exit code 0 regardless of whether it finds some files or whether it returns the "no revision(s) after that date" message. That means you have to parse the output.
The simplest solution is probably to exit the script if $filelist[0] =~ / - no revision\(s\) after that date\./. The downside is we don't know how "stable" that message is. Will future versions of Perforce emit this message exactly, or is it possible they would reword?
Another option is to use the -s switch: my $p4cmd = "p4 -s files -e $files\#$previous_date,\#now";. That causes p4 to prepend the "severity" before every line of output. If a file is found, the line will start with info:, while the "no revision(s) after that date" will start with error:. That looks a bit more stable to me: exit if grep /^error:/, #filelist. Watch out for the last line; when you use the -s switch, you get an extra line with the exit code.
Yet another option would be to use P4Perl. In that case you'd get the results as structured data, which will obviate the parsing. That's arguably the most elegant, but you'd need the P4Perl module first.
I suggest using the -F flag to tame the output:
my $p4cmd = "p4 -F %depotFile% files -e $files\#$previous_date,\#now";
and then go ahead with the:
my #filelist = `$p4cmd`;
good_bye() unless #filelist; # Say goodbye and exit.
#filelist will be empty if there are no lines of output containing a %depotFile% field, and now your caller doesn't need to try to parse the depot path out of the standard p4 files output.
If you want to massage the p4 files output further, take a look at p4 -e files (args) so you can see what the different fields are that you can plug into -F.
Just do nothing if the array isn't populated.
my #filelist = `$p4cmd`;
good_bye() unless #filelist; # Say goodbye and exit.
chomp #filelist;
To suppress the message, just redirect stderr of the command to a bitbucket:
my $p4cmd = "p4 files -e $files\#$previous_date,\#now 2> /dev/null";

Calling another function in fish shell

I received this excellent answer on how to convert a zsh function to a fish function. Now I have another question. How do I call that function from another function, passing on the argument?
I have tried this:
function ogf
echo "Cloning, your editor will open when clone has completed..."
source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
end
function wogf
env EDITOR=webstorm ogf "$argv[1]"
end
but I get "env: ogf: No such file or directory".
The goal is only to change the EDITOR environment variable for this one execution, and then call ogf.
The env command can only run other external commands. It cannot call shell builtins or functions; regardless whether the shell is fish, bash, or something else. The solution is to define the function being called with the --no-scope-shadowing flag and use set -l in the calling function:
function ogf --no-scope-shadowing
echo "Cloning, your editor will open when clone has completed..."
source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
end
function wogf
set -l EDITOR webstorm
ogf $argv
end
Another option would be to write your function to use its own arguments, as follows:
function ogf
echo "Cloning, your editor will open when clone has completed..."
source (env TARGET_DIRECTORY=~/students EDITOR=$argv[2] clone_git_file -ts $argv[1] | psub)
end
function wogf
ogf $argv[1] 'webstorm'
end
Maybe this is a simpler example on how to call another function while passing arguments:
function foo
bar "hello" "world"
end
function bar
echo $argv[1]
echo $argv[2]
end
Then calling foo will print:
$ foo
hello
world

SVN Pre-Commit hook

I am new to svn.Svn repository is in Linux,and developers are working on windows using TSVN client.I implemented a per-commit hook with a proper comment of 32 characters.it is working in Linux.But i tried in TSVN client to commit the code with comment is less than 32 characters it is working.Can any one help me on this.
Here is the code:
$minchars = 10;
$svnlook = '/usr/bin/svnlook';
#--------------------------------------------
$repos = $ARGV[0];
$txn = $ARGV[1];
$comment = `$svnlook log -t "$txn" "$repos"`;
chomp($comment);
if ( length($comment) == 0 ) {
print STDERR "A comment is required!";
exit(1);
} elsif ( length($comment) < $minchars ) {
print STDERR "Comment must be at least $minchars characters.";
exit(1);
}
exit(0);
Try this:
Copy your script to another directory and modify it to use the -r parameter for the svnlook command rather than -t. Then, try it with a commit revision that should have failed.
For example:
$ cd $repo_dir/hooks
$ cp pre-commit $HOME
$ cd
$ vim pre-commit #Change from Transaction to Revision
$ # Revision #123 should have failed
$ ./pre-commit $repo $rev
If the script doesn't produce an error, you can try such things as printing out the comment in quotes to see whether or not it's zero in length, etc. It'll help you find the possible logic error in your script.
You should also use use strict; and use warnings; in your Perl scripts because it easily picks up errors you might not realize you have in your script. It's so easy to forget that a particular variable wasn't necessarily set, or that you mistyped a variable. These pragmas will pick up these types of errors which seem to cause about 90% of the problems in Perl:
#! /usr/bin/env perl
use strict;
use warnings;
my $svnlook = "/usr/bin/svnlook";
my $minchars = 10;
my $repos = $ARGV[0];
my $txn = $ARGV[1];
chomp ( my $comment = qx($svnlook log -t $txn $repos) );
if (not $comment) {
die "A comment is required!\n";
}
elsif ( length $comment < $minchars ) {
die "Comment must be at least $minchars characters.\n";
}
exit 0;
You can also use my pre-commit script. It can be used to verify the length and structure of the commit comment. For example, you could require the commit comment to require a defect ID. It also allows you to control who has commit rights in what parts of your repository and also enforce the use of certain properties on certain files. For example, you might want to make sure all shell scripts and Perl scripts have the svn:eol-style set to either native or LF.
It can also allow users to create a tag, but not allow users to make changes in a tag once created. This prevents users from accidentally checking out a tag, making a change, and then committing it.
And, one more thing:
Take a look at a continuous build system such as Jenkins. One of the things I've discovered is that by merely doing continuous builds, developers naturally improve their commit messages without doing any sort of enforcement.
That's because commit messages are now easily visible. Jenkins shows the changes in each build, whether the build itself was successful, test results, etc. It shows the changes and the commit comments. Suddenly, the commit comments become much more useful to the developers themselves, and they simply do better comments.
You can look at an svn log and see when I implemented Jenkins: Before there were either no commit comments, or such useful things as "reformatted code" or the very helpful "made changes" (both longer than 10 characters). Suddenly the comments are "Fixed BUG-1233. Checked for null pointer before passing it to foo method".