ru Русский

Reticularium

NETWORKS PLACE

Well, quite self-explanatory. Only Bash and Ruby. Options “folder” and “file” are examples, replace them with what you need… Shared in hope this can help someone.

Bash template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash

possible_options="folder, file"
necessary_options="file"

# The 3 lines below are needed if the script needs other files located in the same folder.
# This finds the real folder even if the script is symlinked.
[ -h $0 ] && xcommand=`readlink $0` || xcommand=$0
rcommand=${xcommand##*/}   # The real script
rpath=${xcommand%/*}   # The real folder
[ "X$*" == "X" ] && echo "Can't run without options. Possible options are: ${possible_options}" && exit 1
for s_option in "${@}"
do
  found=0
  case ${s_option} in
  --*=*)
    s_optname=`expr "X$s_option" : 'X[^-]*-*\([^=]*\)'`  
    s_optarg=`expr "X$s_option" : 'X[^=]*=\(.*\)'` 
    ;;
  --*)
    s_optname=`expr "X$s_option" : 'X[^-]*-*\([^=]*\)'`    
    s_optarg='yes' 
    ;;
  *=*|*)
    echo "Wrong syntax: options must start with a double dash"
    exit 1
    ;;
  esac
  for option in `echo $possible_options | sed 's/,//g'`; do 
    [ "X$s_optname" == "X$option" ] && eval "$option=${s_optarg}" && found=1
  done
  if [[ found -ne 1 ]]; then 
    echo "Unknown option: $s_optname"
    exit 1
  fi
done
found=0

for option in `echo $necessary_options | sed 's/,//g'`; do
  [ "X$(eval echo \$$option)" == "X" ] && missing_options="${missing_options}, --${option}" && found=1
done
if [[ found -eq 1 ]]; then
  missing_options=${missing_options#*,}
  echo "Necessary options: ${missing_options} not found"
  exit 1
fi

### Here goes your code

Ruby template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env ruby

possible_options = "folder, file"
necessary_options = "file"
options = Hash.new
possible_options.split(',').each { |po| options[po.strip] = '' }
File.symlink?(__FILE__) ? xcommand = File.readlink(__FILE__) : xcommand = File.path(__FILE__)
rcommand = File.basename(xcommand)
rpath = File.dirname(xcommand)
begin
  if ARGV.empty?
    raise(ArgumentError, "Can't run without options. Possible options are: #{possible_options}")
  else
    for s_option in ARGV
      (s_option =~ /--/) == 0 || raise(ArgumentError, "Wrong syntax: options must start with a double dash")
      (s_option =~ /=/).nil? ? s_optarg = "yes" : s_optarg = s_option.split("=")[1]
      s_optname = s_option.split('=')[0].gsub('--','')
      if options.include?(s_optname)
        options[s_optname] = s_optarg
      else
        raise(ArgumentError, "Unknown option: #{s_optname}")
        exit 1
      end
    end
    necessary_options.split(',').each do |o|
      if options[o].empty?
        raise(ArgumentError, "Option --#{o} is necessary")
        exit 1
      end
    end
  end

### Here goes your code
### note that command-line options are in hash! Address them as e.g. options[file]

rescue Exception => e
  puts e.message
  exit 1
end

I’d only recommend to expand usage/help information.

Comment it: