瀏覽代碼

Better way to parse args

Alois Mahdal 10 年之前
父節點
當前提交
3bb78fb7ea
共有 1 個檔案被更改,包括 15 行新增5 行删除
  1. 15
    5
      bin/mkexec

+ 15
- 5
bin/mkexec 查看文件

@@ -5,7 +5,6 @@ use strict;
5 5
 use warnings;
6 6
 
7 7
 sub usage; sub mkexec; sub guesstype; sub getcmd; sub launch_editor;
8
-sub nextarg { $ARGV[0] }
9 8
 
10 9
 my $DEFAULT_TYPE = 'sh';
11 10
 my $DEFAULT_MODE = 0755;
@@ -16,10 +15,21 @@ my $DEFAULT_FORCE = 0;
16 15
 ## INIT ## -------------------------------------------------------------------
17 16
 ## '''' ##   ' ' '  '   '    '     '      '       '        '         '
18 17
 
19
-defined nextarg() or usage;
20
-my $force   = (nextarg() eq '-f' ? shift : $DEFAULT_FORCE);
21
-my $name    = (defined nextarg() ? shift : usage);
22
-my $type    = (defined nextarg() ? shift : guesstype $name);
18
+my $force = $DEFAULT_FORCE;
19
+my $name; my $type;
20
+
21
+foreach (@ARGV) {
22
+    if (m/-f|--force/) {
23
+        $force++;
24
+    } elsif (defined $name) {
25
+        $type = $_;
26
+    } else {
27
+        $name = $_;
28
+    }
29
+}
30
+
31
+usage unless defined $name;
32
+$type = guesstype $name unless defined $type;
23 33
 
24 34
 my $bangs = {
25 35
     pl   => `which perl`,