diff options
Diffstat (limited to 'tprint/src/Options.hs')
-rw-r--r-- | tprint/src/Options.hs | 115 |
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 | |||
4 | module Options | ||
5 | ( TPrint(..) | ||
6 | , Operation(..) | ||
7 | , Input, Output | ||
8 | , Format(..), Source(..), Sink(..) | ||
9 | , withArgs | ||
10 | , module Options.Utils | ||
11 | ) where | ||
12 | |||
13 | import Data.Time | ||
14 | |||
15 | import GHC.Generics (Generic) | ||
16 | |||
17 | import Text.Show.Pretty (PrettyVal) | ||
18 | |||
19 | import Thermoprint.Client | ||
20 | import Options.Applicative | ||
21 | |||
22 | import Options.Utils | ||
23 | import Instances () | ||
24 | |||
25 | data TPrint = TPrint | ||
26 | { baseUrl :: BaseUrl | ||
27 | , dryRun :: Bool | ||
28 | , output :: Output | ||
29 | , operation :: Operation | ||
30 | , dumpOptions :: Bool | ||
31 | } | ||
32 | deriving (Show, Generic, PrettyVal) | ||
33 | |||
34 | data 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 | |||
66 | type Input = (Format, Source) | ||
67 | type Output = (Format, Sink ) | ||
68 | |||
69 | data Format = Human | BBCode | Internal | JSON | ||
70 | deriving (Enum, Bounded, Show, Read, Generic, PrettyVal) | ||
71 | |||
72 | data Source = Stdin | ReadFile FilePath | ||
73 | deriving (Show, Generic, PrettyVal) | ||
74 | |||
75 | data Sink = Stdout | WriteFile FilePath | ||
76 | deriving (Show, Generic, PrettyVal) | ||
77 | |||
78 | cmdPrinters :: ParserInfo Operation | ||
79 | cmdPrinters = info cmdPrinters' $ mconcat [ header "List all available printers" | ||
80 | ] | ||
81 | where cmdPrinters' = pure Printers | ||
82 | |||
83 | cmdJobs :: ParserInfo Operation | ||
84 | cmdJobs = 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 | |||
92 | pOperation :: Parser Operation | ||
93 | pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters | ||
94 | , command "jobs" cmdJobs | ||
95 | ] | ||
96 | |||
97 | pTPrint :: Parser TPrint | ||
98 | pTPrint = 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 | |||
104 | pOutput :: Parser Output | ||
105 | pOutput = (,) <$> 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 | |||
114 | withArgs :: (TPrint -> IO a) -> IO a | ||
115 | withArgs a = customExecParser (prefs $ showHelpOnError) (info pTPrint $ header "tprint - A cli interface to Thermoprint.Client") >>= a | ||