用cmdargs排序的参数 [英] Ordered arguments with cmdargs

查看:139
本文介绍了用cmdargs排序的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,它可以通过 cmdargs 获得一些参数。
我想检索文件路径列表和要执行的操作列表。我需要这些文件,然后按照顺序执行这些操作

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"
        }

我设法按照 String FilePath )的列表获得文件路径列表。通过这种方式,我可以得到我的输入文件:

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"

但是我不能让我的行为按照他们被声明为 Bool 的顺序排列。
我想传递这样的参数:

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"

获得不同的输出结果。

cmdargs是否可以按顺序检索不同的参数? strong>

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

推荐答案


cmdargs是否可以按顺序检索不同参数?

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

是的,使用 enum

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

这篇关于用cmdargs排序的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆