Loading...

Csh Function-like Utility

Apps1yrs ago (2023)update ICSteve
404 0

Cshell does not have native function support, and also does not have too much fancy stuff, like variable domain, etc.

Csh Function-like Utility

But the function is really a must have one for the cshrc start up program.

Goal

  • Call the method like use a script
  • Apply the setting to the current cshell environment.
  • Easy to get help or apply the auto-completion
  • Integrate with our cshrc structure
  • Support argument when calling the function

Here are some good practices coding the function-like file in cshell.

  • Provide an option called "–program". So user can easily change the name of the "function".
  • Provide the following boolean options
    • is_help_mode
    • is_debug_mode
    • is_dryrun_mode
    • is_complete_mode
  • Code the auto-complete in the file
  • Use while to loop the argv
  • Pay attention to some special behaviors, like will exit command exit the whole program, etc.

Template

#!/usr/bin/env tcsh

# Use this program like a csh function.
# Once you saved this file to $__MYSH_ROOT/cshsub/myecho.csh
# alias "myecho" "source $__MYSH_ROOT/cshsub/myecho.csh --program myecho \!*"
# Example: myecho -pcolor "blue" -prefix "MY-TEST" -color yellow "This is my message." 

# Initialize the environment context
set program = $0 ;
set is_help_mode = 0 ;
set is_debug_mode = 0 ;
set is_dryrun_mode = 0
set is_complete_mode = 0 ;

# Prefix text color
set pcolor = "" ;
# Message body color
set color = "" ;
# Default prefix text
set prefix = "MY-INFO" ;

set argc = $#argv ;
set i    = 1 ;
set argnomatch = "" ;
while ($i <= $argc)
  set is_argmatch = 0 ;
  set nexti = `expr $i + 1` ;
  # Bool: Help mode
  if ("$argv[$i]" == "-h" || "$argv[$i]" == "-help" || "$argv[$i]" == "--help") then
    set is_help_mode = 1 ;
    set is_argmatch = 1 ;
  endif
  # Bool: Set the debug mode when -debug is present
  if ("$argv[$i]" == "--debug") then
    set is_debug_mode = 1 ;
	 set is_argmatch = 1 ;
  endif
  # Bool: Set the debug mode when -dryrun is present
  if ("$argv[$i]" == "--dryrun") then
    set is_dryrun_mode = 1 ;
	 set is_argmatch = 1 ;
  endif
  # Bool: Set the debug mode when -dryrun is present
  if ("$argv[$i]" == "--complete") then
    set is_complete_mode = 1 ;
	 set is_argmatch = 1 ;
  endif
  # Param: Set the program name from the argument, so no link is needed
  if ("$argv[$i]" == "--program") then
    set program = $argv[$nexti] ;
    set i = $nexti ;
		set is_argmatch = 1 ;
  endif
  # -------------------------------------------------------------------------------------------------------------------
  # Bool: Keep the tmp git repo if -keep_tmp is present
  # default is 0, tmp git repo will be removed
  if ("$argv[$i]" == "-color") then
    set color = $argv[$nexti] ;
		set i = $nexti
		set is_argmatch = 1 ;
  endif
  if ("$argv[$i]" == "-pcolor") then
    set pcolor = $argv[$nexti] ;
		set i = $nexti
		set is_argmatch = 1 ;
  endif

  if ("$argv[$i]" == "-prefix") then
    set prefix = $argv[$nexti] ;
		set i = $nexti
		set is_argmatch = 1 ;
  endif
  # -------------------------------------------------------------------------------------------------------------------
  if ($is_argmatch == 0) then
    set argnomatch = "$argnomatch $argv[$i]" ;
	endif 
	set i = `expr $i + 1` ;
end
set colors = ("red" "green" "yellow" "blue" "magenta" "cyan" "white" "black") ;
if ("$pcolor" != "") then
  set is_pcolor_available = 0 ;
  foreach each_color ($colors)
    if ("$each_color" == "$pcolor") then
      set is_pcolor_available = 1 ;
    endif
  end
  if ($is_pcolor_available == 0) then
    echo "Available color code are:\n$colors\nPlease correct and rerun, exiting myecho function ..." ;
    exit ;
  endif
endif
if ("$color" != "") then
  set is_color_available = 0 ;
  foreach each_color ($colors)
    if ("$each_color" == "$color") then
      set is_color_available = 1 ;
    endif
  end
  if ($is_color_available == 0) then
    echo "Available color code are:\n$colors\nPlease correct and rerun, exiting myecho function ..." ;
    exit ;
  endif
endif
set program = `basename $program` ;

# Print out the help message
if ($is_help_mode > 0) then
  echo "\n$program [options]\n  rsync files from local git repo to deployment area\n" ;
  exit ;
endif

if ($is_complete_mode > 0) then
  echo "Setting the command auto completion." ;
  set __myexec_option_words = (help h complete debug dryrun color pcolor prefix) ;
  complete $program \
    'c/-/$__myexec_option_words/'

  exit ;
  echo "This message should not be printed out since it is after the exit command" ;
endif

if ($is_debug_mode == 1) then
  # Debug mode settings
  echo $mycolor_magenta"MY-DEBUG: "$mycolor_end"Current program is "$program ;
else
  # Non-debug mode settings
endif

if ("$color" != "") then
  set color_var = "mycolor_"$color ;
  if ("$pcolor" != "") then
    set pcolor_var = "mycolor_"$pcolor
  	eval echo \$"{"$pcolor_var"}"{$prefix}":"$mycolor_end \$"{"$color_var"}"$argnomatch $mycolor_end ;
  else
    set echo_cmdstr = "{$prefix}: \$argnomatch"
    eval "echo "$echo_cmdstr
  endif
else
  set echo_body = $argnomatch ;
  if ("$pcolor" != "") then
    set color_var = "mycolor_"$pcolor
  	eval echo \$"{"$color_var"}"{$prefix}":"$mycolor_end $argnomatch ;
  else
    set echo_cmdstr = "{$prefix}: \$argnomatch"
    eval "echo "$echo_cmdstr
  endif
endif
© Copyright notes

Related posts

No comments

No comments...