3

I would like to create a program who get some arguments with cmdargs. I would like to retrieve a list of filepath and list of actions to do. I needs these files to be taken and these actions to be perfomed in order.

My arguments are declared like this :

data Options = 
    Mode1   { 
              input :: [FilePath]
            , act1 :: Bool
            , act2 :: Bool
            , act3 :: Bool
            } deriving (Data, Typeable, Show, Eq)

mode1 = 
  Mode1 { 
            input   = []      &= name "i" &= help "input file"   &= typ "FILE"
          , act1    = False   &= name "a" &= help "action 1"     &= typ "ACTION"
          , act2    = False   &= name "b" &= help "action 2"     &= typ "ACTION"
          , act3    = False   &= name "c" &= help "action 3"     &= typ "ACTION"
        }

I managed to get the list of filepath in order with a list of String (FilePath). In this way I can get my input files ordered with :

./myprog --input="file1.txt" --input="file2.txt"
./myprog --input="file2.txt" --input="file1.txt"

But I can't have my actions to be ordered as far as they are declared as Bool. I would like to pass my arguments like this :

./myprog --act1 --act2 --act3 --input="file1.txt" --input="file2.txt"
./myprog --act3 --act1        --input="file1.txt" --input="file2.txt"
./myprog --act2 --act3 --act1 --input="file1.txt" --input="file2.txt"

to get differents output results.

Is it possible with cmdargs to retrieve differents arguments in order ?

JeanJouX
  • 2,555
  • 1
  • 25
  • 37
  • Looks like these were removed in the last few versions http://hackage.haskell.org/package/cmdargs-0.10.13/docs/System-Console-CmdArgs-GetOpt.html#v:Permute – Arnon Aug 29 '15 at 17:44

1 Answers1

1

Is it possible with cmdargs to retrieve differents arguments in order ?

Yes, with enum.

$ runhaskell ~/testargs.hs -a -b -1 "~" -c -a
Mode1 {input = ["~"], acts = [ActA,ActB,ActC,ActA]}

Using :

{-# LANGUAGE DeriveDataTypeable #-}
import System.Console.CmdArgs

data Act = 
    ActA
  | ActB
  | ActC
  deriving (Show, Typeable,  Data, Eq)

data Options = Mode1
  { input :: [FilePath]
  , acts :: [Act]
  } deriving (Show, Data, Typeable, Eq)

actsDef :: [Act]
actsDef = (enum
  [ [] &= ignore
  , [ActA] &= name "a" &= help "action a"
  , [ActB] &= name "b" &= help "action b" 
  , [ActC] &= name "c" &= help "action c" 
  ]) &= typ "ACTION"

mode :: Options
mode = Mode1
  { input = [] &= name "1" &= help "input file" &= typ "FILE"
  , acts = actsDef 
  }

main = print =<< cmdArgs mode
gxtaillon
  • 1,016
  • 1
  • 19
  • 33