本文介绍了使ReadArgs 1.0可以使用一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用,似乎它不支持单参数情况。

Playing around with the ReadArgs package, it seems that it does not support single-argument situations.

{-# LANGUAGE ScopedTypeVariables #-}

import ReadArgs (readArgs)

main = do
  (foo :: Int) <- readArgs
  print foo

错误是(使用版本1.0时):

The error is (when using version 1.0):

No instance for (ReadArgs.ArgumentTuple Int)
  arising from a use of `readArgs'

我的问题是双重的:


  1. readArgs 的工作方式

  2. 被调整以允许它与单个参数一起工作?
  1. How does readArgs work?
  2. How can that library be adjusted to allow it to work with a single argument as well?

NB ReadArgs的1.1版消除了这个错误;

N.B. version 1.1 of ReadArgs eliminates this "error"; see comments.

推荐答案

我不太了解所有需要启用的扩展,但是您可以定义一个实例 ReadArgs.ArgumentTuple a (即使它不是一个真正的语义上正确的名称),如下所示:

I don't quite understand all of the extensions I needed to enable, but you could define an instance of ReadArgs.ArgumentTuple a (even though it's not really a semantically correct name) like this:

{-# LANGUAGE FlexibleInstances, UndecidableInstances,
             OverlappingInstances, ScopedTypeVariables #-}

import ReadArgs

instance (Argument a) => ArgumentTuple a where
  parseArgsFrom ss = do
    a :& () <- parseArgsFrom ss
    return a
  usageFor a = usageFor (a :& ())

main = do
    (foo :: Int) <- readArgs
    print foo

另外,我不确定是否有这个实例的任何问题,尽管它适用于您提供的示例。我会认为这是从图书馆丢失的原因,但我可能是错的。

Also, I'm not really sure if there are any problems with this instance, though it works for the example you presented. I would assume there's a reason it's missing from the library, but I may be wrong.

之后为了确保它们仍然有效(如下面的例子),我相信这不会导致任何问题,所以也许它(或者类似的)存在只是一个疏忽。

After looking trying a few things out, to be sure they still work (like the following example), I'm fairly convinced this doesn't cause any problems, so maybe it's (or something similar) existance was just an oversight.

main = do
    (foo :: Int, bar :: Int) <- readArgs
    print foo
    print bar

这篇关于使ReadArgs 1.0可以使用一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:36