-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup commandline arguments to be more sensible
- Loading branch information
Andrew Nguyen
committed
Sep 30, 2024
1 parent
38de17d
commit e99322b
Showing
8 changed files
with
217 additions
and
122 deletions.
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 |
---|---|---|
@@ -1,6 +1,15 @@ | ||
# bf-hs | ||
|
||
### Run instructions | ||
- `stack run -- -i FILENAME_GOES_HERE` this is without `.b` | ||
- `stack run -- -i FILENAME_GOES_HERE` | ||
- or `stack run` which defaults to hello_world | ||
- `./output/FILENAME_GOES_HERE` | ||
|
||
### Flags and Arguments | ||
```manpage | ||
-i Provide input file | ||
-O Provide output directory | ||
-T Provide temporary output directory (this is for intermediate files) | ||
-I Provide input directory (this is ignored if `-i` is set) | ||
-d Print debug information | ||
``` |
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
Empty file.
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ dependencies: | |
- base >= 4.7 && < 5 | ||
- process | ||
- directory | ||
- filepath | ||
|
||
ghc-options: | ||
- -Wall | ||
|
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,56 @@ | ||
module CommandLine ( | ||
ArgOption, | ||
MaybeOr (..), | ||
opt, | ||
flag, | ||
parseArgs, | ||
) where | ||
|
||
import Control.Monad (guard, join) | ||
import Data.Foldable (Foldable (..)) | ||
import Data.List (find) | ||
import Utils | ||
|
||
type ArgOption a = [String] -> Maybe (a, [String]) | ||
|
||
{- MaybeOr -} | ||
newtype MaybeOr a = MaybeOr {getMaybeOr :: Maybe a} | ||
deriving (Show, Eq, Functor) | ||
|
||
instance Semigroup (MaybeOr a) where | ||
(MaybeOr x) <> (MaybeOr y) = MaybeOr $ maybe y pure x | ||
|
||
instance Monoid (MaybeOr a) where | ||
mempty = MaybeOr Nothing | ||
|
||
instance Applicative MaybeOr where | ||
pure = MaybeOr . Just | ||
(<*>) = liftA2 ($) | ||
|
||
unfoldArgs :: (Eq a) => [ArgOption a] -> [String] -> Either String [a] | ||
unfoldArgs opts = go | ||
where | ||
go = \case | ||
[] -> pure [] | ||
xs@(x : _) -> do | ||
(a, rs) <- maybeToEither ("invalid argument: " <> x) $ f opts xs | ||
(a :) <$> go rs | ||
f = join . find (Nothing /=) .* sequence | ||
|
||
parseArgs :: (Monoid a, Eq a) => [ArgOption a] -> [String] -> Either String a | ||
parseArgs pa args = fold <$> unfoldArgs pa args | ||
|
||
{- Utils -} | ||
opt :: String -> (String -> a) -> [String] -> Maybe (a, [String]) | ||
opt = \cases | ||
match f (op : r : rs) -> do | ||
guard $ startsWith match op | ||
pure (f r, rs) | ||
_ _ _ -> Nothing | ||
|
||
flag :: String -> a -> [String] -> Maybe (a, [String]) | ||
flag = \cases | ||
m c (r : rs) -> do | ||
guard $ startsWith m r | ||
pure (c, rs) | ||
_ _ _ -> Nothing |
Oops, something went wrong.