module Options (ItCliCommand(..), parseProgArgs) where import Data.ItCli import System.Directory as Sd import Options.Applicative as Op import Control.Applicative import Data.Monoid data ItCliCommand = Open IssueTitle | Close IssueIdArg | CommentCommand IssueIdArg CommentMessage | Init | ListIssues ListFilterArgs | ShowIssue IssueIdArg | Version | Branch IssueIdArg | CommitMessgae IssueIdArg | IssuePath Bool IssueIdArg deriving (Show, Eq, Ord) parseProgArgs :: IO ItCliCommand parseProgArgs = Op.execParser parseItCliInfo parseItCliInfo :: Op.ParserInfo ItCliCommand parseItCliInfo = Op.info (helper <*> parseItCli) $ Op.fullDesc <> Op.header "itcli - issue tracker for cli" parseItCli :: Op.Parser ItCliCommand parseItCli = parseInitCmd <|> parseOpenCmd <|> parseCloseCmd <|> parseListIssuesCmd <|> parseCommentCmd <|> parseShowIssueCmd <|> parseBranchCmd <|> parseCommitMsgCmd <|> parsePathCmd <|> parseVersionCmd parseVersionCmd :: Op.Parser ItCliCommand parseVersionCmd = makeCommand "version" "Print version info" (pure Version) parsePathCmd :: Op.Parser ItCliCommand parsePathCmd = makeCommand "path" "Outputs the full path to the given issue" (IssuePath <$> parsePathRelativeOpt <*> parseIssueId) parseCommitMsgCmd :: Op.Parser ItCliCommand parseCommitMsgCmd = makeCommand "commit" "Generates a commit message for closing an issue" (CommitMessgae <$> parseIssueId) parseBranchCmd :: Op.Parser ItCliCommand parseBranchCmd = makeCommand "branch" "Generates a branch name for use with git for the given issue" (Branch <$> parseIssueId) parseInitCmd :: Op.Parser ItCliCommand parseInitCmd = makeCommand "init" "init the cwd with the issue tracker" (pure Init) parseListIssuesCmd :: Op.Parser ItCliCommand parseListIssuesCmd = makeCommand "list" "list the issues" (ListIssues <$> parseStatusFilterFlags) parseShowIssueCmd :: Op.Parser ItCliCommand parseShowIssueCmd = makeCommand "show" "shows the comments for the given issue" (ShowIssue <$> parseIssueId) parseOpenCmd :: Op.Parser ItCliCommand parseOpenCmd = makeCommand "open" "open a new issue with the given title (limited to 70 chars)" (Open <$> parseIssueTitle) parseCloseCmd :: Op.Parser ItCliCommand parseCloseCmd = makeCommand "close" "close a new command" (Close <$> parseIssueId) parseCommentCmd :: Op.Parser ItCliCommand parseCommentCmd = makeCommand "add" "append a comment or add a file attachment to an issue" (CommentCommand <$> parseIssueId <*> parseCommentMessage) parseIssueTitle :: Op.Parser IssueTitle parseIssueTitle = flip Op.argument (Op.metavar "TITLE" <> Op.help "the title of the issue (limited to 70 chars)" ) $ take 70 <$> Op.str parseCommentMessage :: Op.Parser CommentMessage parseCommentMessage = commentMessageFromString <$> Op.strArgument (Op.metavar "MSG | FILEPATH" <> Op.help "Either a comment or filepath. Files are copied to the issues directory" ) parseIssueId :: Op.Parser IssueIdArg parseIssueId = Op.strArgument (Op.metavar "ISSUE-ID" <> Op.help "The hash id prefix of the issue to operate on." ) parseStatusFilterFlags :: Op.Parser (Maybe Bool) parseStatusFilterFlags = Op.flag Nothing (Just True) (Op.short 'c' <> Op.long "closed") <|> Op.flag Nothing (Just False) (Op.short 'o' <> Op.long "open") parsePathRelativeOpt :: Op.Parser Bool parsePathRelativeOpt = Op.switch $ Op.short 'r' <> Op.long "relative" <> Op.help "show path relative to parent dir of the .itcli dir" makeCommand :: String -> String -> Op.Parser a -> Op.Parser a makeCommand c h p = Op.hsubparser $ (Op.metavar c <>) $ Op.command c $ info p $ progDesc h