-
-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -450,6 +450,7 @@ src/hledger/ | |
Register.md | ||
Rewrite.md | ||
Roi.md | ||
Run.md | ||
Stats.md | ||
Tags.md | ||
Test.md | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{-| | ||
The @run@ command allows you to run multiple commands via REPL or from the supplied file(s). | ||
-} | ||
|
||
{-# LANGUAGE MultiWayIf #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Hledger.Cli.Commands.Run ( | ||
runmode | ||
,run | ||
,run' | ||
) where | ||
|
||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as T | ||
import System.Console.CmdArgs.Explicit as C ( Mode ) | ||
import Hledger | ||
import Hledger.Cli.CliOptions | ||
|
||
import Control.Monad (forM_) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Control.Monad.Extra (concatMapM) | ||
|
||
import System.Console.Haskeline | ||
|
||
import Safe (headMay) | ||
|
||
-- | Command line options for this command. | ||
runmode = hledgerCommandMode | ||
$(embedFileRelative "Hledger/Cli/Commands/Run.txt") | ||
( | ||
[] | ||
) | ||
cligeneralflagsgroups1 | ||
hiddenflags | ||
([], Just $ argsFlag "[COMMANDS_FILE1 COMMANDS_FILE2 ...]") | ||
|
||
-- | The fake run command introduced to break circular dependency | ||
run' :: CliOpts -> Journal -> IO () | ||
run' _opts _j = return () | ||
|
||
-- | The actual run command. | ||
run :: (String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())) -> CliOpts -> Journal -> IO () | ||
run findBuiltinCommand CliOpts{rawopts_=rawopts} j = do | ||
let inputfiles = listofstringopt "args" rawopts | ||
case inputfiles of | ||
[] -> runREPL findBuiltinCommand j | ||
_ -> runFromFiles findBuiltinCommand inputfiles j | ||
|
||
runFromFiles :: (String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())) -> [String] -> Journal -> IO () | ||
runFromFiles findBuiltinCommand inputfiles j = do | ||
dbg1IO "inputfiles" inputfiles | ||
-- read commands from all the inputfiles | ||
commands <- (flip concatMapM) inputfiles $ \f -> do | ||
dbg1IO "reading commands" f | ||
lines . T.unpack <$> T.readFile f | ||
|
||
forM_ commands (runCommand findBuiltinCommand j) | ||
|
||
runCommand :: (String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())) -> Journal -> String -> IO () | ||
runCommand findBuiltinCommand j cmdline = do | ||
dbg1IO "running command" cmdline | ||
-- # begins a comment, ignore everything after # | ||
case takeWhile (not. ((Just '#')==) . headMay) $ words' (strip cmdline) of | ||
"echo":args -> putStrLn $ unwords $ args | ||
cmdname:args -> | ||
case findBuiltinCommand cmdname of | ||
Nothing -> putStrLn $ unwords (cmdname:args) | ||
Just (cmdmode,cmdaction) -> do | ||
opts <- getHledgerCliOpts' cmdmode args | ||
cmdaction opts j | ||
[] -> return () | ||
|
||
runREPL :: (String -> Maybe (Mode RawOpts, CliOpts -> Journal -> IO ())) -> Journal -> IO () | ||
runREPL findBuiltinCommand j = do | ||
putStrLn "Enter hledger commands, or 'help' for help." | ||
runInputT defaultSettings loop | ||
where | ||
loop :: InputT IO () | ||
loop = do | ||
minput <- getInputLine "% " | ||
case minput of | ||
Nothing -> return () | ||
Just "quit" -> return () | ||
Just "exit" -> return () | ||
Just input -> do | ||
liftIO $ runCommand findBuiltinCommand j input | ||
loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
## run | ||
|
||
Runs a sequnce of hledger commands on the same input file(s), either interactively or as a script. | ||
|
||
```flags | ||
Flags: | ||
no command-specific flags | ||
``` | ||
|
||
The commands will run more quickly than if run individually, because the input files would be parsed only once. | ||
|
||
When file names are given to "run", it will read commands from these files, in order. | ||
|
||
With no arguments, "run" will start a read-eval-print loop (REPL) | ||
where you can enter commands interactively. To exit REPL, use "exit" | ||
or "quit", or send EOF. | ||
|
||
Syntax of the commands (either in the file, or in REPL) is intentionally simple: | ||
- each line is a single hledger command | ||
- lines that can't be interpreted as hledger commands are printed out as-is | ||
- empty lines are skipped | ||
- everything after `#` is considered to be a comment and will be ignored, and will not be printed out | ||
- `echo <text>` will print out text, even if it could be recognized as a hledger command | ||
|
||
You can use single quotes or double quotes to quote aguments that need quoting. | ||
|
||
You can use `#!/usr/bin/env hledger run` in the first line of the file to make it a runnable script. | ||
|
||
For example: | ||
|
||
```cli | ||
#!/usr/bin/env hledger run | ||
echo "List of accounts" | ||
accounts | ||
echo "Assets" | ||
balance assets --depth 2 | ||
echo "Liabilities" | ||
balance liabilities --depth 3 --transpose | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
run | ||
|
||
Runs a sequnce of hledger commands on the same input file(s), either | ||
interactively or as a script. | ||
|
||
Flags: | ||
no command-specific flags | ||
|
||
The commands will run more quickly than if run individually, because the | ||
input files would be parsed only once. | ||
|
||
When file names are given to "run", it will read commands from these | ||
files, in order. | ||
|
||
With no arguments, "run" will start a read-eval-print loop (REPL) where | ||
you can enter commands interactively. To exit REPL, use "exit" or | ||
"quit", or send EOF. | ||
|
||
Syntax of the commands (either in the file, or in REPL) is intentionally | ||
simple: - each line is a single hledger command - lines that can't be | ||
interpreted as hledger commands are printed out as-is - empty lines are | ||
skipped - everything after # is considered to be a comment and will be | ||
ignored, and will not be printed out - echo <text> will print out text, | ||
even if it could be recognized as a hledger command | ||
|
||
You can use single quotes or double quotes to quote aguments that need | ||
quoting. | ||
|
||
You can use #!/usr/bin/env hledger run in the first line of the file to | ||
make it a runnable script. | ||
|
||
For example: | ||
|
||
#!/usr/bin/env hledger run | ||
echo "List of accounts" | ||
accounts | ||
|
||
echo "Assets" | ||
balance assets --depth 2 | ||
|
||
echo "Liabilities" | ||
balance liabilities --depth 3 --transpose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters