哈斯克尔。解析命令行参数

发布于 2025-01-29 08:44:09 字数 1103 浏览 2 评论 0原文

我正在学习Haskell编程语言。现在我正在学习IO。我想编写简单的命令行util。 util获取两个文件(JSON或YAML),对它们进行比较并输出比较结果(JSON,YAML,纯文本)。

我认为,我的uter界面是:

my_util "path to first file" "path to second file" -- format JSON. 

我决定使用cmdargs。但是现在,我的界面显示了:

my_util -f="path to first file" -s="path to second file" -- format JSON.

我做了什么。

data Gendiff = Gendiff
    {first_file ::  String
    ,second_file :: String
    ,format :: String
    }
    deriving (Data,Typeable,Show,Eq)
 
gendiff = Gendiff
    {first_file  = def &=  name "f" &= typDir  &= help "path to first file to compare"  &= typ "String"
    ,second_file = def &=  name "s" &=  typDir  &= help  "path to second file to compare" &= typ "String"
    ,format =  &= opt "JSON" &= help "set format of output"  &= typ "String"
    } &=
    verbosity &=
    help "Compares two configuration files and shows a difference." &=
    summary " Gendiff v0.0.1, (C) dosart"
 
main = print =<< cmdArgs gendif

我如何制作第一个变体?如何设置格式标志默认值?

I am learning Haskell programming language. Now I am studying IO. I'd like to write simple command line util. The util take two files (JSON or YAML), compare them and output the result of compare (JSON, YAML, Plain text).

I think, the interface of my util would be that:

my_util "path to first file" "path to second file" -- format JSON. 

I decided to use CmdArgs. But now, my interface show so:

my_util -f="path to first file" -s="path to second file" -- format JSON.

What I was done.

data Gendiff = Gendiff
    {first_file ::  String
    ,second_file :: String
    ,format :: String
    }
    deriving (Data,Typeable,Show,Eq)
 
gendiff = Gendiff
    {first_file  = def &=  name "f" &= typDir  &= help "path to first file to compare"  &= typ "String"
    ,second_file = def &=  name "s" &=  typDir  &= help  "path to second file to compare" &= typ "String"
    ,format =  &= opt "JSON" &= help "set format of output"  &= typ "String"
    } &=
    verbosity &=
    help "Compares two configuration files and shows a difference." &=
    summary " Gendiff v0.0.1, (C) dosart"
 
main = print =<< cmdArgs gendif

How I can make first variant? How to set the format flag default value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2025-02-05 08:44:09

我猜您会使用 args 。这样的事情:

data Gendiff = Gendiff
    { format :: String
    , files :: [String]
    }

gendiff = Gendiff
    { format = def &= -- same as before
    , files = def &= args
    } &= -- same as before

main = do
    args <- cmdArgs gendiff
    case args of
        Gendiff { files = [first, second] } -> -- success path
        _ -> -- print an error

I guess you would use args. Something like this:

data Gendiff = Gendiff
    { format :: String
    , files :: [String]
    }

gendiff = Gendiff
    { format = def &= -- same as before
    , files = def &= args
    } &= -- same as before

main = do
    args <- cmdArgs gendiff
    case args of
        Gendiff { files = [first, second] } -> -- success path
        _ -> -- print an error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文