aboutsummaryrefslogtreecommitdiff
path: root/tprint
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-03-01 08:43:47 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2016-03-01 08:43:47 +0100
commit6bd2bb7117e2b0d2932fdb683a307f91947c2e2b (patch)
treeac9f7e54af90990105d3cf3dbda61886d9ef0ecc /tprint
parentb302f3442a4884029f1839d4dbb25e82358ee758 (diff)
downloadthermoprint-6bd2bb7117e2b0d2932fdb683a307f91947c2e2b.tar
thermoprint-6bd2bb7117e2b0d2932fdb683a307f91947c2e2b.tar.gz
thermoprint-6bd2bb7117e2b0d2932fdb683a307f91947c2e2b.tar.bz2
thermoprint-6bd2bb7117e2b0d2932fdb683a307f91947c2e2b.tar.xz
thermoprint-6bd2bb7117e2b0d2932fdb683a307f91947c2e2b.zip
Complete specification of cl arguments
Diffstat (limited to 'tprint')
-rw-r--r--tprint/src/Options.hs81
-rw-r--r--tprint/src/Options/Utils.hs16
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
23import Options.Utils 23import Options.Utils
24import Instances () 24import Instances ()
25 25
26import Paths_tprint (version)
27import Data.Version (showVersion)
28
26data TPrint = TPrint 29data TPrint = TPrint
27 { baseUrl :: BaseUrl 30 { baseUrl :: BaseUrl
28 , dryRun :: Bool 31 , dryRun :: Bool
@@ -80,28 +83,70 @@ supportedInputs, supportedOutputs :: [Format]
80supportedInputs = [BBCode] 83supportedInputs = [BBCode]
81supportedOutputs = [Human] 84supportedOutputs = [Human]
82 85
83cmdPrinters :: ParserInfo Operation 86cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation
84cmdPrinters = info cmdPrinters' $ mconcat [ header "List all available printers" 87cmdPrinters = info cmdPrinters' $ progDesc "List all available printers"
85 ]
86 where cmdPrinters' = pure Printers 88 where cmdPrinters' = pure Printers
87 89
88cmdJobs :: ParserInfo Operation 90cmdJobs = info cmdJobs' $ progDesc "List printjobs"
89cmdJobs = info cmdJobs' $ mconcat [ header "List printjobs" 91 where cmdJobs' = Jobs
90 ] 92 <$> optional (pPrinter $ help "List only jobs associated with printer #PRINTER")
91 where 93 <*> 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")
92 cmdJobs' = Jobs 94 <*> 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")
93 <$> optional (option (PrinterId <$> auto) $ metavar "PRINTER" <> long "printer" <> short 'p' <> help "List only jobs associated with printer #PRINTER") 95
94 <*> 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") 96cmdJobCreate = info cmdJobCreate' $ progDesc "Queue a new job"
95 <*> 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") 97 where cmdJobCreate' = JobCreate
98 <$> optional (pPrinter $ help "Direct for the job to printed on #PRINTER specifically")
99 <*> pInput
100
101cmdJob = info cmdJob' $ progDesc "Retrieve a jobs contents"
102 where cmdJob' = Job <$> argument (JobId <$> auto) (metavar "JOB")
103
104cmdJobStatus = info cmdJobStatus' $ progDesc "Find a jobs current status"
105 where cmdJobStatus' = JobStatus <$> argument (JobId <$> auto) (metavar "JOB")
106
107cmdJobDelete = info cmdJobDelete' $ progDesc "Prevent a job from being printed"
108 where cmdJobDelete' = JobDelete <$> argument (JobId <$> auto) (metavar "JOB")
109
110cmdDrafts = info cmdDrafts' $ progDesc "List drafts"
111 where cmdDrafts' = pure Drafts
112
113cmdDraftCreate = info cmdDraftCreate' $ progDesc "Create a new draft"
114 where cmdDraftCreate' = DraftCreate <$> optional pTitle <*> pInput
115
116cmdDraftReplace = info cmdDraftReplace' $ progDesc "Update the contents and title of a draft"
117 where cmdDraftReplace' = DraftReplace <$> aDraft <*> optional pTitle <*> pInput
118
119cmdDraft = info cmdDraft' $ progDesc "Retrieve a drafts contents"
120 where cmdDraft' = Draft <$> aDraft
121
122cmdDraftDelete = info cmdDraftDelete' $ progDesc "Delete a draft"
123 where cmdDraftDelete' = DraftDelete <$> aDraft
124
125cmdDraftPrint = info cmdDraftPrint' $ progDesc "Queue a copy of a drafts contents to be printed"
126 where cmdDraftPrint' = DraftPrint <$> aDraft <*> optional (pPrinter $ help "Direct for the drafts contents to be printed on #PRINTER specifically")
96 127
97pOperation :: Parser Operation 128pOperation :: Parser Operation
98pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters 129pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters
99 , command "jobs" cmdJobs 130 , command "jobs" cmdJobs
131 , command "drafts" cmdDrafts
132 , command "job" $ info ( hsubparser $ mconcat [ command "create" cmdJobCreate
133 , command "content" cmdJob
134 , command "status" cmdJobStatus
135 , command "abort" cmdJobDelete
136 ]
137 ) (progDesc "Interact with jobs")
138 , command "draft" $ info ( hsubparser $ mconcat [ command "create" cmdDraftCreate
139 , command "replace" cmdDraftReplace
140 , command "content" cmdDraft
141 , command "delete" cmdDraftDelete
142 , command "print" cmdDraftPrint
143 ]
144 ) (progDesc "Interact with drafts")
100 ] 145 ]
101 146
102pTPrint :: Parser TPrint 147pTPrint :: Parser TPrint
103pTPrint = TPrint <$> option (eitherReader parseBaseUrl) (metavar "URL" <> long "baseurl" <> short 'u' <> help "Server to interact with" <> value (BaseUrl Http "localhost" 3000) <> showDefaultWith showBaseUrl) 148pTPrint = TPrint <$> option (eitherReader parseBaseUrl) (metavar "URL" <> long "baseurl" <> short 'u' <> help "Server to interact with" <> value (BaseUrl Http "localhost" 3000) <> showDefaultWith showBaseUrl)
104 <*> switch (long "dry-run" <> short 'n' <> help "Don't send any requests that would be expected to change the servers state" <> showDefault) 149 <*> switch (long "dry-run" <> short 'n' <> help "Don't send any requests that would be expected to change the servers state")
105 <*> pOutput 150 <*> pOutput
106 <*> pOperation 151 <*> pOperation
107 <*> switch (long "dump-options" <> internal) 152 <*> switch (long "dump-options" <> internal)
@@ -116,5 +161,15 @@ pOutput = (,) <$> pOutputFormat <*> pSink
116 rSink' "-" = Stdout 161 rSink' "-" = Stdout
117 rSink' x = WriteFile x 162 rSink' x = WriteFile x
118 163
164pInput :: Parser Input
165pInput = (,) <$> pInputFormat <*> pSource
166 where
167 pInputFormat = option rCI $ metavar "FORMAT" <> long "input-format" <> short 'f' <> help ("Format to parse as input (possible values: " ++ show supportedInputs ++ ")") <> value (head supportedInputs) <> showDefault
168 pSource = argument rSource $ metavar "PATH" <> help "Read input from PATH (`-' means stdout)" <> value Stdin <> showDefault
169 rSource = rSource' <$> str
170 where
171 rSource' "-" = Stdin
172 rSource' x = ReadFile x
173
119withArgs :: (TPrint -> IO a) -> IO a 174withArgs :: (TPrint -> IO a) -> IO a
120withArgs a = customExecParser (prefs $ showHelpOnError) (info pTPrint $ header "tprint - A cli interface to Thermoprint.Client") >>= a 175withArgs 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
2 ( rCI 2 ( rCI
3 , rTime 3 , rTime
4 , pRange 4 , pRange
5 , pPrinter
6 , aDraft
7 , pTitle
5 ) where 8 ) where
6 9
7import Options.Applicative 10import Options.Applicative
8 11
12import Data.Text (Text)
13import qualified Data.Text as T (pack)
14
9import Data.Char 15import Data.Char
10import Data.Maybe 16import Data.Maybe
11import Data.List 17import Data.List
@@ -13,7 +19,7 @@ import Text.Read
13 19
14import Data.Time 20import Data.Time
15 21
16import Thermoprint.Client (Range(..)) 22import Thermoprint.Client (Range(..), PrinterId(..), DraftId(..), DraftTitle)
17 23
18rCI :: (Read a, Show a) => ReadM a 24rCI :: (Read a, Show a) => ReadM a
19rCI = eitherReader rRep' 25rCI = eitherReader rRep'
@@ -46,3 +52,11 @@ pRange r both min max = toRange <$> optional (option r (both <> min)) <*> option
46 toRange Nothing (Just max) = Just $ Max max 52 toRange Nothing (Just max) = Just $ Max max
47 toRange (Just min) (Just max) = Just $ min `Through` max 53 toRange (Just min) (Just max) = Just $ min `Through` max
48 54
55pPrinter :: Mod OptionFields PrinterId -> Parser PrinterId
56pPrinter mod = option (PrinterId <$> auto) $ metavar "PRINTER" <> long "printer" <> short 'p' <> mod
57
58aDraft :: Parser DraftId
59aDraft = argument (DraftId <$> auto) (metavar "DRAFT")
60
61pTitle :: Parser DraftTitle
62pTitle = option (T.pack <$> auto) $ metavar "TITLE" <> long "title" <> short 't'