Browse Source

Switch FILE an TEMPLATE arguments

With readline's C-., it's convenient to have the name of the new target
be always the last argument.

New syntax follows this convention and still makes the TEMPLATE optional
by using the plus sign as template name decorator.

The effect of the plus-sign use means that having some template names
already starting with plus would bring silly syntax with double-plus
sign.  Having two types of templates is not really all that meaningful
so let's solve that by merely removing `+` from the names and using it
merely in its decorator sense.
Alois Mahdal 6 years ago
parent
commit
97ae354df1
1 changed files with 9 additions and 15 deletions
  1. 9
    15
      bin/mkx

+ 9
- 15
bin/mkx View File

4
 usage() {
4
 usage() {
5
     local self
5
     local self
6
     self="$(basename "$0")"
6
     self="$(basename "$0")"
7
-    warn "usage: $self [-f|--force] FILE [[+]TEMPLATE]"
7
+    warn "usage: $self [-f|--force] [+TEMPLATE] FILE"
8
     warn "usage: $self -l|--list"
8
     warn "usage: $self -l|--list"
9
     exit 2
9
     exit 2
10
 }
10
 }
28
     #
28
     #
29
     # Print list of valid templates
29
     # Print list of valid templates
30
     #
30
     #
31
-    echo +pya
32
-    echo +shellfu
31
+    echo pya
32
+    echo shellfu
33
     echo Bash
33
     echo Bash
34
     echo Lua
34
     echo Lua
35
     echo Perl
35
     echo Perl
41
 #shellcheck disable=SC2016
41
 #shellcheck disable=SC2016
42
 mktemplate() {
42
 mktemplate() {
43
     #
43
     #
44
-    # Print template $1 for $FilePath
44
+    # Print template $Template for $FilePath
45
     #
45
     #
46
-    # Template may be a name of built-in template (starting with
47
-    # plus sign) or language name (actually *interpreter* name).
48
-    #
49
-    local tmplname="${1:-$Template}"
50
-    case "$tmplname" in
46
+    case "$Template" in
51
         sh)
47
         sh)
52
             echo "#!/bin/sh"
48
             echo "#!/bin/sh"
53
             echo ''
49
             echo ''
82
             echo "if __name__ == '__main__':"
78
             echo "if __name__ == '__main__':"
83
             echo '    '
79
             echo '    '
84
             ;;
80
             ;;
85
-        +pya)
81
+        pya)
86
             echo '#!/usr/bin/python'
82
             echo '#!/usr/bin/python'
87
             echo '"""'
83
             echo '"""'
88
             echo 'A simple application'
84
             echo 'A simple application'
120
             echo 'if __name__ == '__main__':'
116
             echo 'if __name__ == '__main__':'
121
             echo '    App.main(sys.argv)'
117
             echo '    App.main(sys.argv)'
122
             ;;
118
             ;;
123
-        +shellfu)
119
+        shellfu)
124
             echo "#!/bin/bash"
120
             echo "#!/bin/bash"
125
             echo ""
121
             echo ""
126
             echo '#shellcheck disable=SC1090'
122
             echo '#shellcheck disable=SC1090'
143
             echo ''
139
             echo ''
144
             echo 'main "$@"'
140
             echo 'main "$@"'
145
             ;;
141
             ;;
146
-        "")
147
-            mktemplate "$(guess_language)"
148
-            ;;
149
         *)
142
         *)
150
             die "unknown template or language: $Template"
143
             die "unknown template or language: $Template"
151
             ;;
144
             ;;
179
         -f|--force) careful=false; shift ;;
172
         -f|--force) careful=false; shift ;;
180
         -l|--list)  lstemplates; exit 0 ;;
173
         -l|--list)  lstemplates; exit 0 ;;
181
         -*) usage ;;
174
         -*) usage ;;
175
+        +*) Template="${1:1}"; shift ;;
182
         *)  break ;;
176
         *)  break ;;
183
     esac done
177
     esac done
184
     FilePath="$1"
178
     FilePath="$1"
185
-    Template="$2"
186
     test -n "$FilePath" || usage
179
     test -n "$FilePath" || usage
187
     test -f "$FilePath" && $careful && die "file already exists: $FilePath"
180
     test -f "$FilePath" && $careful && die "file already exists: $FilePath"
181
+    test -n "$Template" || Template=$(guess_language)
188
     mktemplate > "$FilePath"
182
     mktemplate > "$FilePath"
189
     chmod +x "$FilePath"
183
     chmod +x "$FilePath"
190
 }
184
 }