summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CmdLine/Option.hs4
-rw-r--r--Command/Import.hs56
2 files changed, 30 insertions, 30 deletions
diff --git a/CmdLine/Option.hs b/CmdLine/Option.hs
index 1b82dc83e..2d4e67fa3 100644
--- a/CmdLine/Option.hs
+++ b/CmdLine/Option.hs
@@ -10,6 +10,7 @@ module CmdLine.Option (
flagOption,
fieldOption,
optionName,
+ optionParam,
ArgDescr(..),
OptDescr(..),
) where
@@ -68,3 +69,6 @@ fieldOption short opt paramdesc description =
{- The flag or field name used for an option. -}
optionName :: Option -> String
optionName (Option _ o _ _) = Prelude.head o
+
+optionParam :: Option -> String
+optionParam o = "--" ++ optionName o
diff --git a/Command/Import.hs b/Command/Import.hs
index 305396c7f..025563b49 100644
--- a/Command/Import.hs
+++ b/Command/Import.hs
@@ -21,42 +21,38 @@ cmd = [withOptions opts $ notBareRepo $ command "import" paramPaths seek
SectionCommon "move and add files from outside git working copy"]
opts :: [Option]
-opts =
- [ duplicateOption
- , deduplicateOption
- , cleanDuplicatesOption
- , skipDuplicatesOption
- ] ++ fileMatchingOptions
+opts = duplicateModeOptions ++ fileMatchingOptions
-duplicateOption :: Option
-duplicateOption = flagOption [] "duplicate" "do not delete source files"
-
-deduplicateOption :: Option
-deduplicateOption = flagOption [] "deduplicate" "delete source files whose content was imported before"
+data DuplicateMode = Default | Duplicate | DeDuplicate | CleanDuplicates | SkipDuplicates
+ deriving (Eq, Enum, Bounded)
-cleanDuplicatesOption :: Option
-cleanDuplicatesOption = flagOption [] "clean-duplicates" "delete duplicate source files (import nothing)"
+associatedOption :: DuplicateMode -> Maybe Option
+associatedOption Default = Nothing
+associatedOption Duplicate = Just $
+ flagOption [] "duplicate" "do not delete source files"
+associatedOption DeDuplicate = Just $
+ flagOption [] "deduplicate" "delete source files whose content was imported before"
+associatedOption CleanDuplicates = Just $
+ flagOption [] "clean-duplicates" "delete duplicate source files (import nothing)"
+associatedOption SkipDuplicates = Just $
+ flagOption [] "skip-duplicates" "import only new files"
-skipDuplicatesOption :: Option
-skipDuplicatesOption = flagOption [] "skip-duplicates" "import only new files"
-
-data DuplicateMode = Default | Duplicate | DeDuplicate | CleanDuplicates | SkipDuplicates
- deriving (Eq)
+duplicateModeOptions :: [Option]
+duplicateModeOptions = mapMaybe associatedOption [minBound..maxBound]
getDuplicateMode :: Annex DuplicateMode
-getDuplicateMode = gen
- <$> getflag duplicateOption
- <*> getflag deduplicateOption
- <*> getflag cleanDuplicatesOption
- <*> getflag skipDuplicatesOption
+getDuplicateMode = go . catMaybes <$> mapM getflag [minBound..maxBound]
where
- getflag = Annex.getFlag . optionName
- gen False False False False = Default
- gen True False False False = Duplicate
- gen False True False False = DeDuplicate
- gen False False True False = CleanDuplicates
- gen False False False True = SkipDuplicates
- gen _ _ _ _ = error "bad combination of --duplicate, --deduplicate, --clean-duplicates, --skip-duplicates"
+ getflag m = case associatedOption m of
+ Nothing -> return Nothing
+ Just o -> ifM (Annex.getFlag (optionName o))
+ ( return (Just m)
+ , return Nothing
+ )
+ go [] = Default
+ go [m] = m
+ go ms = error $ "cannot combine " ++
+ unwords (map (optionParam . fromJust . associatedOption) ms)
seek :: CommandSeek
seek ps = do