Changeset 8729 for src/Pugs/Run/Args.hs

Show
Ignore:
Timestamp:
01/17/06 14:55:25 (3 years ago)
Author:
audreyt
Message:

* t/pugsrun/01-multiple-e.t now happily passes.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Run/Args.hs

    r8726 r8729  
    3333  > (-h -v -V) (-I) (-d) (-w) (-c) (-C) (--external) (-M) (-n -p) (-l -0 -e other) 
    3434 
    35   Args -M, -n and -p are converted to -e scripts by joinDashE. 
     35  Args -M, -n and -p are converted to -e scripts by desugarDashE. 
    3636-} 
    3737canonicalArgs :: [String] -> [String] 
    3838canonicalArgs x = concatMap procArg 
    39                 . joinDashE 
     39                . concatDashE 
     40                . desugarDashE 
    4041                . sortBy compareArgs 
    4142                . gatherArgs 
     
    4344                $ x 
    4445 
    45 data Arg = File String | Switch Char | Opt String String 
    46   deriving Show 
     46concatDashE :: [Arg] -> [Arg] 
     47concatDashE (Opt "-e" e:xs) = (Opt "-e" $ concat (intersperse "\n" (e:map optArg es))) : rest 
     48    where 
     49    (es, rest)          = partition isOptE xs 
     50    isOptE (Opt "-e" _) = True 
     51    isOptE _            = False 
     52concatDashE (x:xs) = (x:concatDashE xs) 
     53concatDashE xs = xs 
     54 
     55data Arg 
     56    = File !String 
     57    | Switch !Char 
     58    | Opt { optFlag :: !String, optArg :: !String } 
     59    deriving Show 
    4760 
    4861procArg :: Arg -> [String] 
     
    7083unpackOption opt 
    7184    | Just short <- lookup ('-':opt) longOptions = [short] 
    72     | head opt `elem` composable = ('-':head opt:[]) : unpackOption (tail opt) 
     85    | head opt `elem` composable = ['-', head opt] : unpackOption (tail opt) 
    7386    | Just (prefix, param) <- prefixOpt opt = ['-':prefix, param] 
    7487    | otherwise = ['-':opt] 
     
    151164   and "-p" into "-e" fragments 
    152165-} 
    153 joinDashE :: [Arg] -> [Arg] 
    154 joinDashE [] = [] 
    155 joinDashE ((Switch 'p'):args) = joinDashE ((Opt "-e" "while ($_ = =<>) { $_ .= chomp;"):script++[(Opt "-e" "; say $_; }")]++rest) 
     166desugarDashE :: [Arg] -> [Arg] 
     167desugarDashE [] = [] 
     168desugarDashE ((Switch 'p'):args) = desugarDashE ((Opt "-e" "while ($_ = =<>) { $_ .= chomp;"):script++[(Opt "-e" "; say $_; }")]++rest) 
    156169                                 where 
    157170                                   (script,rest) = partition isDashE args 
    158171                                   isDashE (Opt "-e" _) = True 
    159172                                   isDashE (_) = False 
    160 joinDashE ((Switch 'n'):args) = joinDashE ((Opt "-e" "while ($_ = =<>) { $_ .= chomp;"):script++[(Opt "-e" "}")]++rest) 
     173desugarDashE ((Switch 'n'):args) = desugarDashE ((Opt "-e" "while ($_ = =<>) { $_ .= chomp;"):script++[(Opt "-e" "}")]++rest) 
    161174                                 where 
    162175                                   (script,rest) = partition isDashE args 
     
    168181--   "-e foo bar.p6" executes "foo" with @*ARGS[0] eq "bar.p6", 
    169182--   "-E foo bar.p6" executes "foo" and then bar.p6. 
    170 joinDashE ((Opt "-M" mod):args) = joinDashE ((Opt "-E" (";use " ++ mod ++ ";\n")):args) 
     183desugarDashE ((Opt "-M" mod):args) = desugarDashE ((Opt "-E" (";use " ++ mod ++ ";\n")):args) 
    171184 
    172185-- Preserve the curious Perl5 behaviour: 
    173186--   perl -e 'print CGI->VERSION' -MCGI     # works 
    174187--   perl print_cgi.pl -MCGI                # fails 
    175 joinDashE (x@(Opt "-e" _):y@(Opt "-E" _):args) = joinDashE (y:x:args) 
    176 joinDashE ((Opt "-E" a):y@(Opt "-e" _):args) = joinDashE ((Opt "-e" a):y:args) 
    177  
    178 joinDashE ((Opt "-e" a):(Opt "-e" b):args) = 
    179     joinDashE (Opt "-e" combined:args) 
    180     where 
    181     combined = a++"\n"++b 
    182 joinDashE (x:xs) =  [ x ] ++ joinDashE xs 
     188desugarDashE (x@(Opt "-e" _):y@(Opt "-E" _):args) = desugarDashE (y:x:args) 
     189desugarDashE ((Opt "-E" a):y@(Opt "-e" _):args) = desugarDashE ((Opt "-e" a):y:args) 
     190desugarDashE (x:xs) = (x:desugarDashE xs)