Drkcore

26 12 2012 Haskell Tweet

24 Days of Hackage

24 Days of Hackageを見つけたので楽しく読んでいた。

面白かったのが

  • 24 Days of Hackage: digestive-functors
  • 24 Days of Hackage: optparse-applicative
  • 24 Days of Hackage: blaze-html

特にoptparse-applicativeはpythonのoptparseに似ていて使いやすそうだった。

こんな感じでオプションのパーズとかhelpなんかをよろしくやってくれる。

import Options.Applicative

data Sample = Sample
  { hello :: String
  , quiet :: Bool }

sample :: Parser Sample
sample = Sample
  <$> strOption
      ( long "hello"
      <> metavar "TARGET"
      <> help "Target for the greeting" )
  <*> switch
      ( long "quiet"
      <> help "Whether to be quiet" )

greet :: Sample -> IO ()
greet (Sample h False) = putStrLn $ "Hello, " ++ h
greet _ = return ()

main :: IO ()
main = execParser opts >>= greet
  where
    opts = info (helper <*> sample)
      ( fullDesc
      <> progDesc "Print a greeting for TARGET"
      <> header "hello - a test for optparse-applicative" )

実際にサンプルを動かしてみると使いやすそう

$ ./optest 
Usage: optest --hello TARGET [--quiet]

$ ./optest --hello Haskell
Hello, Haskell

$ ./optest --help
hello - a test for optparse-applicative

Usage: optest --hello TARGET [--quiet]
  Print a greeting for TARGET

Available options:
  -h,--help                Show this help text
  --hello TARGET           Target for the greeting
  --quiet                  Whether to be quiet

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021