diff --git a/README.md b/README.md index 4024286..82e015a 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,19 @@ ``` $ cabal-add --help -Usage: cabal-add [-f|--cabal-file FILE] [-c|--component ARG] DEP +Usage: cabal-add [-f|--cabal-file FILE] ARGS Extend build-depends from the command line Available options: -f,--cabal-file FILE Cabal file to edit in place (tries to detect Cabal file in current folder if omitted). - -c,--component ARG Package component to update (the main library, if - omitted). Wildcards such as 'exe', 'test' or 'bench' - are supported. - DEP Package(s) to add to build-depends section. Version - bounds can be provided as well, use quotes to escape - comparisons from your shell. E. g., 'foo < 0.2'. + ARGS Optional package component (wildcards such as 'exe', + 'test' or 'bench' are supported) to update, followed + by a non-empty list of package(s) to add to + 'build-depends' section. Version bounds can be + provided as well, use quotes to escape comparisons + from your shell. E. g., 'foo < 0.2'. ``` `cabal-add` does not have limitations of diff --git a/app/Main.hs b/app/Main.hs index 0ace4b3..bbd32ee 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -39,8 +39,7 @@ import System.Exit (die) data RawConfig = RawConfig { rcnfMCabalFile :: !(Maybe FilePath) - , rcnfComponent :: !(Maybe String) - , rcnfDependencies :: !(NonEmpty String) + , rcnfArgs :: !(NonEmpty String) } deriving (Show) @@ -53,18 +52,11 @@ parseRawConfig = do <> short 'f' <> metavar "FILE" <> help "Cabal file to edit in place (tries to detect Cabal file in current folder if omitted)." - rcnfComponent <- - optional $ - strOption $ - long "component" - <> short 'c' - <> metavar "ARG" - <> help "Package component to update (the main library, if omitted). Wildcards such as 'exe', 'test' or 'bench' are supported." - rcnfDependencies <- + rcnfArgs <- some1 $ strArgument $ - metavar "DEP" - <> help "Package(s) to add to build-depends section. Version bounds can be provided as well, use quotes to escape comparisons from your shell. E. g., 'foo < 0.2'." + metavar "ARGS" + <> help "Optional package component (wildcards such as 'exe', 'test' or 'bench' are supported) to update, followed by a non-empty list of package(s) to add to 'build-depends' section. Version bounds can be provided as well, use quotes to escape comparisons from your shell. E. g., 'foo < 0.2'." pure RawConfig {..} resolveCabalFileInCurrentFolder :: IO (Either String FilePath) @@ -116,8 +108,13 @@ main = do let inputs = do (fields, packDescr) <- parseCabalFile cabalFile cnfOrigContents let specVer = specVersion $ packageDescription packDescr - cmp <- resolveComponent cabalFile (fields, packDescr) rcnfComponent - deps <- traverse (validateDependency specVer) rcnfDependencies + mkCmp = resolveComponent cabalFile (fields, packDescr) + mkDeps = traverse (validateDependency specVer) + (cmp, deps) <- case rcnfArgs of + x :| (y : ys) + | Right c <- mkCmp (Just x) -> + (c,) <$> mkDeps (y :| ys) + _ -> (,) <$> mkCmp Nothing <*> mkDeps rcnfArgs pure (fields, packDescr, cmp, deps) (cnfFields, origPackDescr, cnfComponent, cnfDependencies) <- case inputs of diff --git a/tests/Main.hs b/tests/Main.hs index f705d14..eff9af8 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -368,7 +368,7 @@ caseSublibraryTarget2 = mkTest $ CabalAddTest { catName = "sublibrary target 2" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -408,7 +408,7 @@ caseExecutableTarget1 = mkTest $ CabalAddTest { catName = "executable target 1" - , catArgs = ["-c", "exe", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["exe", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -442,7 +442,7 @@ caseExecutableTarget2 = mkTest $ CabalAddTest { catName = "executable target 2" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -476,7 +476,7 @@ caseExecutableTarget3 = mkTest $ CabalAddTest { catName = "executable target 3" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -510,7 +510,7 @@ caseExecutableTarget4 = mkTest $ CabalAddTest { catName = "executable target 4" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -550,7 +550,7 @@ caseTestTarget1 = mkTest $ CabalAddTest { catName = "test target 1" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -592,7 +592,7 @@ caseTestTarget2 = mkTest $ CabalAddTest { catName = "test target 2" - , catArgs = ["-c", "test:baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["test:baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -634,7 +634,7 @@ caseCommonStanzaTarget1 = mkTest $ CabalAddTest { catName = "common stanza as a target 1" - , catArgs = ["-c", "foo", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["foo", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -663,7 +663,7 @@ caseCommonStanzaTarget2 = mkTest $ CabalAddTest { catName = "common stanza as a target 2" - , catArgs = ["-c", "foo", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["foo", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -699,7 +699,7 @@ caseTwoSpacesInStanza = mkTest $ CabalAddTest { catName = "two spaces in stanza" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -765,7 +765,7 @@ caseTitleCaseStanza2 = mkTest $ CabalAddTest { catName = "title case in stanza 2" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -799,7 +799,7 @@ caseTitleCaseBuildDepends = mkTest $ CabalAddTest { catName = "title case in build-depends" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -833,7 +833,7 @@ caseSharedComponentPrefixes = mkTest $ CabalAddTest { catName = "shared component prefixes" - , catArgs = ["-c", "baz", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["baz", "foo < 1 && >0.7", "quux < 1"] , catInput = [s| name: dummy @@ -877,7 +877,7 @@ windowsLineEndings = mkTest $ CabalAddTest { catName = "Windows line endings" - , catArgs = ["-c", "exe", "foo < 1 && >0.7", "quux < 1"] + , catArgs = ["exe", "foo < 1 && >0.7", "quux < 1"] , catInput = convertToWindowsLineEndings [s|