Browse Source

Implement a command line parser

tags/v0.2.0.0
Peter J. Jones 6 years ago
parent
commit
c82e2c6190
6 changed files with 100 additions and 1 deletions
  1. 1
    0
      .gitignore
  2. 2
    1
      src/Main.hs
  3. 58
    0
      src/Vimeta/CommandLine.hs
  4. 18
    0
      src/Vimeta/Movie.hs
  5. 20
    0
      src/Vimeta/TV.hs
  6. 1
    0
      vimeta.cabal

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
dist

+ 2
- 1
src/Main.hs View File

@@ -10,6 +10,7 @@ the LICENSE file.
-}

module Main where
import Vimeta.CommandLine (parseAndDispatch)

main :: IO ()
main = putStrLn "Hello World"
main = parseAndDispatch

+ 58
- 0
src/Vimeta/CommandLine.hs View File

@@ -0,0 +1,58 @@
{-

This file is part of the Haskell package vimeta. It is subject to the
license terms in the LICENSE file found in the top-level directory of
this distribution and at git://pmade.com/vimeta/LICENSE. No part of
vimeta package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

module Vimeta.CommandLine (parseAndDispatch) where
import qualified Vimeta.Movie as VM
import qualified Vimeta.TV as VT
import Options.Applicative
import Data.Monoid

-- Possible command line configurations.
data CommandLine = MovieCmd VM.Config
| TVCmd VT.Config
deriving (Show)

-- Parse the configuration for a movie.
movie :: Parser VM.Config
movie = VM.Config
<$> option (short 'i' <> long "movie-id" <> metavar "ID" <>
help "Movie ID assigned by TheMovieDB.org")
<*> argument str (metavar "FILE")

-- Parse the configuration for a TV series.
tv :: Parser VT.Config
tv = VT.Config
<$> option (short 'i' <> long "series" <> metavar "ID" <>
help "TV series ID assigned by TheTVDB.com")
<*> option (short 's' <> long "season" <> metavar "NUM" <>
help "Which season number to store in the episodes")
<*> optional (option (short 'e' <> long "episode" <> metavar "NUM" <>
help "The starting episode number to use"))
<*> arguments1 str (metavar "FILE [FILE...]")

-- Parse all the possible sub-commands.
config :: Parser CommandLine
config = subparser
( command "movie" (info (MovieCmd <$> movie) (progDesc movieDesc))
<> command "tv" (info (TVCmd <$> tv) (progDesc tvDesc))
)
where movieDesc = "Set movie metadata using TheMovieDB"
tvDesc = "Set TV episode metadata using TheTVDB"

-- Dispatch to the correct sub-command.
dispatch :: CommandLine -> IO ()
dispatch (MovieCmd c) = putStrLn $ "Movie: " ++ show c
dispatch (TVCmd c) = putStrLn $ "TV: " ++ show c

-- Actually parse the command line and delegate to the dispatcher.
parseAndDispatch :: IO ()
parseAndDispatch = execParser opts >>= dispatch
where opts = info (config <**> helper) idm

+ 18
- 0
src/Vimeta/Movie.hs View File

@@ -0,0 +1,18 @@
{-

This file is part of the Haskell package vimeta. It is subject to the
license terms in the LICENSE file found in the top-level directory of
this distribution and at git://pmade.com/vimeta/LICENSE. No part of
vimeta package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

module Vimeta.Movie where
import qualified Network.API.TheMovieDB as TMDB

data Config = Config
{ movieID :: TMDB.MovieID
, movieFile :: FilePath
} deriving (Eq, Show)

+ 20
- 0
src/Vimeta/TV.hs View File

@@ -0,0 +1,20 @@
{-

This file is part of the Haskell package vimeta. It is subject to the
license terms in the LICENSE file found in the top-level directory of
this distribution and at git://pmade.com/vimeta/LICENSE. No part of
vimeta package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

module Vimeta.TV where
import Network.API.TheTVDB as TVDB

data Config = Config
{ seriesID :: TVDB.UniqueID
, seasonNumber :: TVDB.UniqueID
, startingEpisodeNumber :: Maybe TVDB.UniqueID
, episodeFiles :: [FilePath]
} deriving (Eq, Show)

+ 1
- 0
vimeta.cabal View File

@@ -29,6 +29,7 @@ executable vimeta
build-depends: base == 4.5.*
, themoviedb == 0.1.*
, thetvdb == 0.1.*
, optparse-applicative == 0.5.*

source-repository head
type: git

Loading…
Cancel
Save