Skip to content

Commit

Permalink
Relax command line parsing: treat the first positional argument as a …
Browse files Browse the repository at this point in the history
…component target; decomission --component
  • Loading branch information
Bodigrim committed Jan 3, 2024
1 parent e388de1 commit a83c827
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down

0 comments on commit a83c827

Please sign in to comment.