diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-03-01 08:43:47 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-03-01 08:43:47 +0100 |
commit | 6bd2bb7117e2b0d2932fdb683a307f91947c2e2b (patch) | |
tree | ac9f7e54af90990105d3cf3dbda61886d9ef0ecc /tprint | |
parent | b302f3442a4884029f1839d4dbb25e82358ee758 (diff) | |
download | thermoprint-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.hs | 81 | ||||
-rw-r--r-- | 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 | |||
23 | import Options.Utils | 23 | import Options.Utils |
24 | import Instances () | 24 | import Instances () |
25 | 25 | ||
26 | import Paths_tprint (version) | ||
27 | import Data.Version (showVersion) | ||
28 | |||
26 | data TPrint = TPrint | 29 | data TPrint = TPrint |
27 | { baseUrl :: BaseUrl | 30 | { baseUrl :: BaseUrl |
28 | , dryRun :: Bool | 31 | , dryRun :: Bool |
@@ -80,28 +83,70 @@ supportedInputs, supportedOutputs :: [Format] | |||
80 | supportedInputs = [BBCode] | 83 | supportedInputs = [BBCode] |
81 | supportedOutputs = [Human] | 84 | supportedOutputs = [Human] |
82 | 85 | ||
83 | cmdPrinters :: ParserInfo Operation | 86 | cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation |
84 | cmdPrinters = info cmdPrinters' $ mconcat [ header "List all available printers" | 87 | cmdPrinters = info cmdPrinters' $ progDesc "List all available printers" |
85 | ] | ||
86 | where cmdPrinters' = pure Printers | 88 | where cmdPrinters' = pure Printers |
87 | 89 | ||
88 | cmdJobs :: ParserInfo Operation | 90 | cmdJobs = info cmdJobs' $ progDesc "List printjobs" |
89 | cmdJobs = 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") | 96 | cmdJobCreate = 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 | |||
101 | cmdJob = info cmdJob' $ progDesc "Retrieve a jobs contents" | ||
102 | where cmdJob' = Job <$> argument (JobId <$> auto) (metavar "JOB") | ||
103 | |||
104 | cmdJobStatus = info cmdJobStatus' $ progDesc "Find a jobs current status" | ||
105 | where cmdJobStatus' = JobStatus <$> argument (JobId <$> auto) (metavar "JOB") | ||
106 | |||
107 | cmdJobDelete = info cmdJobDelete' $ progDesc "Prevent a job from being printed" | ||
108 | where cmdJobDelete' = JobDelete <$> argument (JobId <$> auto) (metavar "JOB") | ||
109 | |||
110 | cmdDrafts = info cmdDrafts' $ progDesc "List drafts" | ||
111 | where cmdDrafts' = pure Drafts | ||
112 | |||
113 | cmdDraftCreate = info cmdDraftCreate' $ progDesc "Create a new draft" | ||
114 | where cmdDraftCreate' = DraftCreate <$> optional pTitle <*> pInput | ||
115 | |||
116 | cmdDraftReplace = info cmdDraftReplace' $ progDesc "Update the contents and title of a draft" | ||
117 | where cmdDraftReplace' = DraftReplace <$> aDraft <*> optional pTitle <*> pInput | ||
118 | |||
119 | cmdDraft = info cmdDraft' $ progDesc "Retrieve a drafts contents" | ||
120 | where cmdDraft' = Draft <$> aDraft | ||
121 | |||
122 | cmdDraftDelete = info cmdDraftDelete' $ progDesc "Delete a draft" | ||
123 | where cmdDraftDelete' = DraftDelete <$> aDraft | ||
124 | |||
125 | cmdDraftPrint = 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 | ||
97 | pOperation :: Parser Operation | 128 | pOperation :: Parser Operation |
98 | pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters | 129 | pOperation = 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 | ||
102 | pTPrint :: Parser TPrint | 147 | pTPrint :: Parser TPrint |
103 | pTPrint = TPrint <$> option (eitherReader parseBaseUrl) (metavar "URL" <> long "baseurl" <> short 'u' <> help "Server to interact with" <> value (BaseUrl Http "localhost" 3000) <> showDefaultWith showBaseUrl) | 148 | pTPrint = 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 | ||
164 | pInput :: Parser Input | ||
165 | pInput = (,) <$> 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 | |||
119 | withArgs :: (TPrint -> IO a) -> IO a | 174 | withArgs :: (TPrint -> IO a) -> IO a |
120 | withArgs a = customExecParser (prefs $ showHelpOnError) (info pTPrint $ header "tprint - A cli interface to Thermoprint.Client") >>= a | 175 | 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 | |||
2 | ( rCI | 2 | ( rCI |
3 | , rTime | 3 | , rTime |
4 | , pRange | 4 | , pRange |
5 | , pPrinter | ||
6 | , aDraft | ||
7 | , pTitle | ||
5 | ) where | 8 | ) where |
6 | 9 | ||
7 | import Options.Applicative | 10 | import Options.Applicative |
8 | 11 | ||
12 | import Data.Text (Text) | ||
13 | import qualified Data.Text as T (pack) | ||
14 | |||
9 | import Data.Char | 15 | import Data.Char |
10 | import Data.Maybe | 16 | import Data.Maybe |
11 | import Data.List | 17 | import Data.List |
@@ -13,7 +19,7 @@ import Text.Read | |||
13 | 19 | ||
14 | import Data.Time | 20 | import Data.Time |
15 | 21 | ||
16 | import Thermoprint.Client (Range(..)) | 22 | import Thermoprint.Client (Range(..), PrinterId(..), DraftId(..), DraftTitle) |
17 | 23 | ||
18 | rCI :: (Read a, Show a) => ReadM a | 24 | rCI :: (Read a, Show a) => ReadM a |
19 | rCI = eitherReader rRep' | 25 | rCI = 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 | ||
55 | pPrinter :: Mod OptionFields PrinterId -> Parser PrinterId | ||
56 | pPrinter mod = option (PrinterId <$> auto) $ metavar "PRINTER" <> long "printer" <> short 'p' <> mod | ||
57 | |||
58 | aDraft :: Parser DraftId | ||
59 | aDraft = argument (DraftId <$> auto) (metavar "DRAFT") | ||
60 | |||
61 | pTitle :: Parser DraftTitle | ||
62 | pTitle = option (T.pack <$> auto) $ metavar "TITLE" <> long "title" <> short 't' | ||