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.hs24
1 files changed, 17 insertions, 7 deletions
diff --git a/tprint/src/Options.hs b/tprint/src/Options.hs
index e146f91..703c23b 100644
--- a/tprint/src/Options.hs
+++ b/tprint/src/Options.hs
@@ -26,8 +26,12 @@ import Instances ()
26import Paths_tprint (version) 26import Paths_tprint (version)
27import Data.Version (showVersion) 27import Data.Version (showVersion)
28 28
29import Data.Maybe
30import Data.Monoid
29import Data.Bifunctor (Bifunctor(..)) 31import Data.Bifunctor (Bifunctor(..))
30 32
33import System.Environment (lookupEnv)
34
31data TPrint = TPrint 35data TPrint = TPrint
32 { baseUrl :: BaseUrl 36 { baseUrl :: BaseUrl
33 , dryRun :: Bool 37 , dryRun :: Bool
@@ -149,12 +153,15 @@ pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters
149 ) (progDesc "Interact with drafts") 153 ) (progDesc "Interact with drafts")
150 ] 154 ]
151 155
152pTPrint :: Parser TPrint 156pTPrint :: IO (Parser TPrint)
153pTPrint = TPrint <$> option (eitherReader $ first show . parseBaseUrl) (metavar "URL" <> long "baseurl" <> short 'u' <> help "Server to interact with" <> value (BaseUrl Http "localhost" 3000 "") <> showDefaultWith showBaseUrl) 157pTPrint = do
154 <*> switch (long "dry-run" <> short 'n' <> help "Don't send any requests that would be expected to change the servers state") 158 baseUrl <- parseBaseUrl =<< (fromMaybe "http://localhost:3000/" <$> lookupEnv "TPRINT_BASEURL")
155 <*> pOutput 159 return $
156 <*> pOperation 160 TPrint <$> option (eitherReader $ first show . parseBaseUrl) (metavar "URL" <> long "baseurl" <> short 'u' <> help "Server to interact with; also read from TPRINT_BASEURL when set" <> value baseUrl <> showDefaultWith showBaseUrl)
157 <*> switch (long "dump-options" <> internal) 161 <*> switch (long "dry-run" <> short 'n' <> help "Don't send any requests that would be expected to change the servers state")
162 <*> pOutput
163 <*> pOperation
164 <*> switch (long "dump-options" <> internal)
158 165
159pOutput :: Parser Output 166pOutput :: Parser Output
160pOutput = (,) <$> pOutputFormat <*> pSink 167pOutput = (,) <$> pOutputFormat <*> pSink
@@ -177,4 +184,7 @@ pInput = (,) <$> pInputFormat <*> pSource
177 rSource' x = ReadFile x 184 rSource' x = ReadFile x
178 185
179withArgs :: (TPrint -> IO a) -> IO a 186withArgs :: (TPrint -> IO a) -> IO a
180withArgs a = customExecParser (prefs $ showHelpOnError) (info pTPrint $ header ("tprint " ++ showVersion version) <> progDesc "A cli for Thermoprint.Client") >>= a 187withArgs a = do
188 pTPrint' <- pTPrint
189 customExecParser (prefs $ showHelpOnError) (info pTPrint' $ header ("tprint " ++ showVersion version) <> progDesc "A cli for Thermoprint.Client") >>= a
190