Skip to content

Commit

Permalink
CLI command parser
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mueller committed Dec 20, 2024
1 parent da928b4 commit 01c808a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/exe/wst-poc-cli/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main where

import Wst.Cli qualified

main :: IO ()
main = Wst.Cli.runMain
6 changes: 0 additions & 6 deletions src/exe/wst-poc/Main.hs

This file was deleted.

15 changes: 14 additions & 1 deletion src/lib/Wst/Cli.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
module Wst.Cli(runMain) where

import Options.Applicative (customExecParser, disambiguate, helper, idm, info,
prefs, showHelpOnEmpty, showHelpOnError)
import Wst.Cli.Command (Command, parseCommand)

runMain :: IO ()
runMain = putStrLn "Starting stablecoin POC server"
runMain = do
customExecParser
(prefs $ disambiguate <> showHelpOnEmpty <> showHelpOnError)
(info (helper <*> parseCommand) idm)
>>= runCommand

runCommand :: Command -> IO ()
runCommand com = do
putStrLn "runCommand"
print com
71 changes: 71 additions & 0 deletions src/lib/Wst/Cli/Command.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{-| The CLI commands and parsers
-}
module Wst.Cli.Command(
parseCommand,
Command(..),
ManageCommand(..)
) where

import Cardano.Api (TxIn (..), TxIx (..))
import Control.Monad (when)
import Data.String (IsString (..))
import Options.Applicative (CommandFields, Mod, Parser, ReadM, argument,
command, eitherReader, fullDesc, help, info, long,
many, metavar, optional, progDesc, short, str,
strOption, subparser, (<|>))
import Text.Read (readMaybe)


parseCommand :: Parser Command
parseCommand =
subparser $
mconcat
[ parseDeploy
, parseManage
]

data Command =
Deploy
| Manage TxIn ManageCommand
deriving Show

-- | Commands that require a deployed system
data ManageCommand =
Status
deriving stock Show

parseDeploy :: Mod CommandFields Command
parseDeploy =
command "deploy" $
info (pure Deploy) (fullDesc <> progDesc "Deploy the directory and global params")

parseManage :: Mod CommandFields Command
parseManage =
command "manage" $
info (Manage <$> parseTxIn <*> parseManageCommand) (fullDesc <> progDesc "Manage a deployed system")

parseManageCommand :: Parser ManageCommand
parseManageCommand = subparser $ mconcat [parseStatus]

parseStatus :: Mod CommandFields ManageCommand
parseStatus =
command "status" $
info (pure Status) (fullDesc <> progDesc "Show the status of the programmable tokens")

parseTxIn :: Parser TxIn
parseTxIn =
argument
txInReader
(help "The TxIn that was selected when deploying the system. Format: <tx-id>.<index>" <> metavar "TX_IN")

txInReader :: ReadM TxIn
txInReader = eitherReader $ \str -> do
(txId, txIx) <- case break ((==) '.') str of
(txId, _:txIx) -> Right (txId, txIx)
_ -> Left "Expected <tx-id>.<index>"
-- 8c728a68fed42fe4893fb84ee2c6276a25e642d9892962af3234f952ea641993
when (length txId /= 64) $ Left "Expected tx ID with 64 characters"
ix <- case readMaybe @Word txIx of
Nothing -> Left "Expected tx index"
Just n -> Right (TxIx n)
return $ TxIn (fromString txId) ix
6 changes: 4 additions & 2 deletions src/wst-poc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ library
SmartTokens.Types.PTokenDirectory
Types.Constants
Wst.Cli
Wst.Cli.Command
Wst.Client
Wst.Offchain
Wst.Offchain.BuildTx.Blacklist
Expand Down Expand Up @@ -104,6 +105,7 @@ library
, generics-sop
, lens
, mtl
, optparse-applicative
, plutarch
, plutarch-ledger-api
, plutarch-onchain-lib
Expand All @@ -119,10 +121,10 @@ library

hs-source-dirs: lib

executable wst-poc
executable wst-poc-cli
import: lang
main-is: Main.hs
hs-source-dirs: exe/wst-poc
hs-source-dirs: exe/wst-poc-cli
build-depends:
, base
, wst-poc
Expand Down

0 comments on commit 01c808a

Please sign in to comment.