From 6bd2bb7117e2b0d2932fdb683a307f91947c2e2b Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 1 Mar 2016 08:43:47 +0100 Subject: Complete specification of cl arguments --- tprint/src/Options.hs | 81 +++++++++++++++++++++++++++++++++++++-------- tprint/src/Options/Utils.hs | 16 ++++++++- 2 files changed, 83 insertions(+), 14 deletions(-) diff --git a/tprint/src/Options.hs b/tprint/src/Options.hs index e9dcfe6..4b61d1c 100644 --- a/tprint/src/Options.hs +++ b/tprint/src/Options.hs @@ -23,6 +23,9 @@ import Options.Applicative import Options.Utils import Instances () +import Paths_tprint (version) +import Data.Version (showVersion) + data TPrint = TPrint { baseUrl :: BaseUrl , dryRun :: Bool @@ -80,28 +83,70 @@ supportedInputs, supportedOutputs :: [Format] supportedInputs = [BBCode] supportedOutputs = [Human] -cmdPrinters :: ParserInfo Operation -cmdPrinters = info cmdPrinters' $ mconcat [ header "List all available printers" - ] +cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation +cmdPrinters = info cmdPrinters' $ progDesc "List all available printers" where cmdPrinters' = pure Printers -cmdJobs :: ParserInfo Operation -cmdJobs = info cmdJobs' $ mconcat [ header "List printjobs" - ] - where - cmdJobs' = Jobs - <$> optional (option (PrinterId <$> auto) $ metavar "PRINTER" <> long "printer" <> short 'p' <> help "List only jobs associated with printer #PRINTER") - <*> pRange (JobId <$> auto) (metavar "JOB") (long "min-job" <> short 'j' <> help "List only jobs with id greater than or equal to JOB") (long "max-job" <> short 'J' <> help "List only jobs with id less than or equal to JOB") - <*> pRange rTime (metavar "TIME") (long "min-time" <> short 't' <> help "List only jobs created after or at TIME") (long "max-time" <> short 'T' <> help "List only jobs created before or at TIME") +cmdJobs = info cmdJobs' $ progDesc "List printjobs" + where cmdJobs' = Jobs + <$> optional (pPrinter $ help "List only jobs associated with printer #PRINTER") + <*> pRange (JobId <$> auto) (metavar "JOB") (long "min-job" <> short 'j' <> help "List only jobs with id greater than or equal to JOB") (long "max-job" <> short 'J' <> help "List only jobs with id less than or equal to JOB") + <*> pRange rTime (metavar "TIME") (long "min-time" <> short 't' <> help "List only jobs created after or at TIME") (long "max-time" <> short 'T' <> help "List only jobs created before or at TIME") + +cmdJobCreate = info cmdJobCreate' $ progDesc "Queue a new job" + where cmdJobCreate' = JobCreate + <$> optional (pPrinter $ help "Direct for the job to printed on #PRINTER specifically") + <*> pInput + +cmdJob = info cmdJob' $ progDesc "Retrieve a jobs contents" + where cmdJob' = Job <$> argument (JobId <$> auto) (metavar "JOB") + +cmdJobStatus = info cmdJobStatus' $ progDesc "Find a jobs current status" + where cmdJobStatus' = JobStatus <$> argument (JobId <$> auto) (metavar "JOB") + +cmdJobDelete = info cmdJobDelete' $ progDesc "Prevent a job from being printed" + where cmdJobDelete' = JobDelete <$> argument (JobId <$> auto) (metavar "JOB") + +cmdDrafts = info cmdDrafts' $ progDesc "List drafts" + where cmdDrafts' = pure Drafts + +cmdDraftCreate = info cmdDraftCreate' $ progDesc "Create a new draft" + where cmdDraftCreate' = DraftCreate <$> optional pTitle <*> pInput + +cmdDraftReplace = info cmdDraftReplace' $ progDesc "Update the contents and title of a draft" + where cmdDraftReplace' = DraftReplace <$> aDraft <*> optional pTitle <*> pInput + +cmdDraft = info cmdDraft' $ progDesc "Retrieve a drafts contents" + where cmdDraft' = Draft <$> aDraft + +cmdDraftDelete = info cmdDraftDelete' $ progDesc "Delete a draft" + where cmdDraftDelete' = DraftDelete <$> aDraft + +cmdDraftPrint = info cmdDraftPrint' $ progDesc "Queue a copy of a drafts contents to be printed" + where cmdDraftPrint' = DraftPrint <$> aDraft <*> optional (pPrinter $ help "Direct for the drafts contents to be printed on #PRINTER specifically") pOperation :: Parser Operation pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters , command "jobs" cmdJobs + , command "drafts" cmdDrafts + , command "job" $ info ( hsubparser $ mconcat [ command "create" cmdJobCreate + , command "content" cmdJob + , command "status" cmdJobStatus + , command "abort" cmdJobDelete + ] + ) (progDesc "Interact with jobs") + , command "draft" $ info ( hsubparser $ mconcat [ command "create" cmdDraftCreate + , command "replace" cmdDraftReplace + , command "content" cmdDraft + , command "delete" cmdDraftDelete + , command "print" cmdDraftPrint + ] + ) (progDesc "Interact with drafts") ] pTPrint :: Parser TPrint pTPrint = TPrint <$> option (eitherReader parseBaseUrl) (metavar "URL" <> long "baseurl" <> short 'u' <> help "Server to interact with" <> value (BaseUrl Http "localhost" 3000) <> showDefaultWith showBaseUrl) - <*> switch (long "dry-run" <> short 'n' <> help "Don't send any requests that would be expected to change the servers state" <> showDefault) + <*> switch (long "dry-run" <> short 'n' <> help "Don't send any requests that would be expected to change the servers state") <*> pOutput <*> pOperation <*> switch (long "dump-options" <> internal) @@ -116,5 +161,15 @@ pOutput = (,) <$> pOutputFormat <*> pSink rSink' "-" = Stdout rSink' x = WriteFile x +pInput :: Parser Input +pInput = (,) <$> pInputFormat <*> pSource + where + pInputFormat = option rCI $ metavar "FORMAT" <> long "input-format" <> short 'f' <> help ("Format to parse as input (possible values: " ++ show supportedInputs ++ ")") <> value (head supportedInputs) <> showDefault + pSource = argument rSource $ metavar "PATH" <> help "Read input from PATH (`-' means stdout)" <> value Stdin <> showDefault + rSource = rSource' <$> str + where + rSource' "-" = Stdin + rSource' x = ReadFile x + withArgs :: (TPrint -> IO a) -> IO a -withArgs a = customExecParser (prefs $ showHelpOnError) (info pTPrint $ header "tprint - A cli interface to Thermoprint.Client") >>= a +withArgs a = customExecParser (prefs $ showHelpOnError) (info pTPrint $ header ("tprint " ++ showVersion version) <> progDesc "A cli for Thermoprint.Client") >>= a diff --git a/tprint/src/Options/Utils.hs b/tprint/src/Options/Utils.hs index 80b4a7e..2bb7fc6 100644 --- a/tprint/src/Options/Utils.hs +++ b/tprint/src/Options/Utils.hs @@ -2,10 +2,16 @@ module Options.Utils ( rCI , rTime , pRange + , pPrinter + , aDraft + , pTitle ) where import Options.Applicative +import Data.Text (Text) +import qualified Data.Text as T (pack) + import Data.Char import Data.Maybe import Data.List @@ -13,7 +19,7 @@ import Text.Read import Data.Time -import Thermoprint.Client (Range(..)) +import Thermoprint.Client (Range(..), PrinterId(..), DraftId(..), DraftTitle) rCI :: (Read a, Show a) => ReadM a rCI = eitherReader rRep' @@ -46,3 +52,11 @@ pRange r both min max = toRange <$> optional (option r (both <> min)) <*> option toRange Nothing (Just max) = Just $ Max max toRange (Just min) (Just max) = Just $ min `Through` max +pPrinter :: Mod OptionFields PrinterId -> Parser PrinterId +pPrinter mod = option (PrinterId <$> auto) $ metavar "PRINTER" <> long "printer" <> short 'p' <> mod + +aDraft :: Parser DraftId +aDraft = argument (DraftId <$> auto) (metavar "DRAFT") + +pTitle :: Parser DraftTitle +pTitle = option (T.pack <$> auto) $ metavar "TITLE" <> long "title" <> short 't' -- cgit v1.2.3