aboutsummaryrefslogtreecommitdiff
path: root/tprint/src/Options.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tprint/src/Options.hs')
-rw-r--r--tprint/src/Options.hs115
1 files changed, 115 insertions, 0 deletions
diff --git a/tprint/src/Options.hs b/tprint/src/Options.hs
new file mode 100644
index 0000000..96bbde6
--- /dev/null
+++ b/tprint/src/Options.hs
@@ -0,0 +1,115 @@
1{-# LANGUAGE StandaloneDeriving #-}
2{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
3
4module Options
5 ( TPrint(..)
6 , Operation(..)
7 , Input, Output
8 , Format(..), Source(..), Sink(..)
9 , withArgs
10 , module Options.Utils
11 ) where
12
13import Data.Time
14
15import GHC.Generics (Generic)
16
17import Text.Show.Pretty (PrettyVal)
18
19import Thermoprint.Client
20import Options.Applicative
21
22import Options.Utils
23import Instances ()
24
25data TPrint = TPrint
26 { baseUrl :: BaseUrl
27 , dryRun :: Bool
28 , output :: Output
29 , operation :: Operation
30 , dumpOptions :: Bool
31 }
32 deriving (Show, Generic, PrettyVal)
33
34data Operation
35 = Printers
36 | Jobs
37 { printer :: Maybe PrinterId
38 , jobRange :: Maybe (Range JobId)
39 , timeRange :: Maybe (Range UTCTime)
40 }
41 | JobCreate
42 { printer :: Maybe PrinterId
43 , input :: Input
44 }
45 | Job { jobId :: JobId }
46 | JobStatus { jobId :: JobId }
47 | JobDelete { jobId :: JobId }
48 | Drafts
49 | DraftCreate
50 { draftTitle :: Maybe DraftTitle
51 , input :: Input
52 }
53 | DraftReplace
54 { draftId :: DraftId
55 , draftTitle :: Maybe DraftTitle
56 , input :: Input
57 }
58 | Draft { draftId :: DraftId }
59 | DraftDelete { draftId :: DraftId }
60 | DraftPrint
61 { draftId :: DraftId
62 , printer :: Maybe PrinterId
63 }
64 deriving (Show, Generic, PrettyVal)
65
66type Input = (Format, Source)
67type Output = (Format, Sink )
68
69data Format = Human | BBCode | Internal | JSON
70 deriving (Enum, Bounded, Show, Read, Generic, PrettyVal)
71
72data Source = Stdin | ReadFile FilePath
73 deriving (Show, Generic, PrettyVal)
74
75data Sink = Stdout | WriteFile FilePath
76 deriving (Show, Generic, PrettyVal)
77
78cmdPrinters :: ParserInfo Operation
79cmdPrinters = info cmdPrinters' $ mconcat [ header "List all available printers"
80 ]
81 where cmdPrinters' = pure Printers
82
83cmdJobs :: ParserInfo Operation
84cmdJobs = info cmdJobs' $ mconcat [ header "List printjobs"
85 ]
86 where
87 cmdJobs' = Jobs
88 <$> optional (option (PrinterId <$> auto) $ metavar "PRINTER" <> long "printer" <> short 'p' <> help "List only jobs associated with printer #PRINTER")
89 <*> 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")
90 <*> 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")
91
92pOperation :: Parser Operation
93pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters
94 , command "jobs" cmdJobs
95 ]
96
97pTPrint :: Parser TPrint
98pTPrint = TPrint <$> option (eitherReader parseBaseUrl) (metavar "URL" <> long "baseurl" <> short 'u' <> help "Server to interact with" <> value (BaseUrl Http "localhost" 3000) <> showDefaultWith showBaseUrl)
99 <*> switch (long "dry-run" <> short 'n' <> help "Don't send any requests that would be expected to change the servers state" <> showDefault)
100 <*> pOutput
101 <*> pOperation
102 <*> switch (long "dump-options" <> internal)
103
104pOutput :: Parser Output
105pOutput = (,) <$> pOutputFormat <*> pSink
106 where
107 pOutputFormat = option rCI $ metavar "FORMAT" <> long "output-format" <> short 'O' <> help ("Format to use for all output (possible values: " ++ show ([minBound..maxBound] :: [Format]) ++ ")") <> value Human <> showDefault
108 pSink = option rSink $ metavar "PATH" <> long "output" <> short 'o' <> help "Send output to PATH (`-' means stdout)" <> value Stdout <> showDefault
109 rSink = rSink' <$> str
110 where
111 rSink' "-" = Stdout
112 rSink' x = WriteFile x
113
114withArgs :: (TPrint -> IO a) -> IO a
115withArgs a = customExecParser (prefs $ showHelpOnError) (info pTPrint $ header "tprint - A cli interface to Thermoprint.Client") >>= a