file name as option in commands

This commit is contained in:
aarne
2008-06-12 21:04:16 +00:00
parent 4369e67998
commit 3b15ade685
2 changed files with 27 additions and 4 deletions

View File

@@ -200,9 +200,14 @@ allCommands pgf = Map.fromList [
("ps", emptyCommandInfo { ("ps", emptyCommandInfo {
longname = "put_string", longname = "put_string",
synopsis = "return a string, possibly processed with a function", synopsis = "return a string, possibly processed with a function",
exec = \opts -> explanation = unlines [
return . fromString . maybe id id (stringOp (concatMap prOpt opts)) . toString, "Returns a string obtained by its argument string by applying",
flags = ["cat","lang"] "string processing functions in the order given in the command line",
"option list. Thus 'ps -f -g s' returns g (f s). Typical string processors",
"are lexers and unlexers."
],
exec = \opts -> return . fromString . stringOps opts . toString,
options = ["lextext","lexcode","lexmixed","unlextext","unlexcode","unlexmixed"]
}), }),
("q", emptyCommandInfo { ("q", emptyCommandInfo {
longname = "quit", longname = "quit",
@@ -228,12 +233,22 @@ allCommands pgf = Map.fromList [
fromTrees [t | Just t <- [readExp s]] fromTrees [t | Just t <- [readExp s]]
_ | isOpt "lines" opts -> fromStrings $ lines s _ | isOpt "lines" opts -> fromStrings $ lines s
_ -> fromString s _ -> fromString s
}),
("wf", emptyCommandInfo {
longname = "write_file",
synopsis = "send string or tree to a file",
exec = \opts arg -> do
let file = valIdOpts "file" "_gftmp" opts
writeFile file (toString arg)
return void
}) })
] ]
where where
lin opts t = unlines [linearize pgf lang t | lang <- optLangs opts] lin opts t = unlines [linearize pgf lang t | lang <- optLangs opts]
par opts s = concat [parse pgf lang (optCat opts) s | lang <- optLangs opts] par opts s = concat [parse pgf lang (optCat opts) s | lang <- optLangs opts]
void = ([],[])
optLin opts t = case opts of optLin opts t = case opts of
_ | isOpt "treebank" opts -> unlines $ (abstractName pgf ++ ": " ++ showExp t) : _ | isOpt "treebank" opts -> unlines $ (abstractName pgf ++ ": " ++ showExp t) :
[lang ++ ": " ++ linea lang t | lang <- optLangs opts] [lang ++ ": " ++ linea lang t | lang <- optLangs opts]
@@ -266,3 +281,7 @@ allCommands pgf = Map.fromList [
morphos opts s = morphos opts s =
[lookupMorpho (buildMorpho pgf (mkCId la)) s | la <- optLangs opts] [lookupMorpho (buildMorpho pgf (mkCId la)) s | la <- optLangs opts]
-- ps -f -g s returns g (f s)
stringOps opts s = foldr app s (reverse (map prOpt opts)) where
app f = maybe id id (stringOp f)

View File

@@ -5,6 +5,7 @@ import PGF.Data(Exp)
import GF.Command.Abstract import GF.Command.Abstract
import Data.Char import Data.Char
import Control.Monad
import qualified Text.ParserCombinators.ReadP as RP import qualified Text.ParserCombinators.ReadP as RP
readCommandLine :: String -> Maybe CommandLine readCommandLine :: String -> Maybe CommandLine
@@ -32,6 +33,9 @@ pOption = do
RP.option (OOpt flg) (fmap (OFlag flg) (RP.char '=' >> pValue)) RP.option (OOpt flg) (fmap (OFlag flg) (RP.char '=' >> pValue))
pValue = do pValue = do
fmap VId pIdent fmap VId pFilename
RP.<++ RP.<++
fmap (VInt . read) (RP.munch1 isDigit) fmap (VInt . read) (RP.munch1 isDigit)
pFilename = liftM2 (:) (RP.satisfy isFileFirst) (RP.munch (not . isSpace)) where
isFileFirst c = not (isSpace c) && not (isDigit c)