Skip to content

Commit

Permalink
Parse Arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
shterrett committed Nov 10, 2018
1 parent e14f777 commit 08d69b5
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 28 deletions.
8 changes: 6 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Main where

import Data.String.Strip
import Args
import Options.Applicative (execParser)

main :: IO ()
main = interact strip
main = execParser app >>= run

run :: Args -> IO ()
run = putStrLn . show
9 changes: 8 additions & 1 deletion escape-from-itunes.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ cabal-version: >=1.10

library
hs-source-dirs: src
exposed-modules: Data.String.Strip
exposed-modules: Itunes
, Args
, Attributes
build-depends: base >= 4.7 && < 5
, idiii == 0.1.3.3
, optparse-applicative == 0.14.3.0
, regex-compat == 0.95.1
default-language: Haskell2010

executable escape-from-itunes
Expand All @@ -26,16 +30,19 @@ executable escape-from-itunes
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, escape-from-itunes
, optparse-applicative == 0.14.3.0
default-language: Haskell2010

test-suite escape-from-itunes-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
other-modules: ArgsSpec
build-depends: base
, escape-from-itunes
, hspec
, QuickCheck
, optparse-applicative == 0.14.3.0
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010

Expand Down
39 changes: 39 additions & 0 deletions src/Args.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Args where

import Options.Applicative
import Data.Semigroup ((<>))
import Text.Regex (mkRegex, splitRegex)
import Attributes

data Args = Args {
source :: String
, target :: String
, attributes :: [Attribute]
}
deriving (Show, Eq)

getArgs :: Parser Args
getArgs = Args
<$> strOption
( long "source" <>
short 's' <>
help "Root of existing library")
<*> strOption
( long "target" <>
short 't' <>
help "Root of new library")
<*> attrList

attrList :: Parser [Attribute]
attrList = option (maybeReader attrList) (
long "attributes" <>
short 'a' <>
help "comma-separated list of orderedf attributes for building directory tree")
where attrList = sequence . (fmap toAttribute) . (splitRegex (mkRegex ","))
helpString = "comma-separated list of ordered attributes for building directory tree. Chosen from artist, title, album, year, track, composer"

app :: ParserInfo Args
app = info (getArgs <**> helper)
( fullDesc
<> progDesc "Copies an iTunes music library to a new directory tree built from the ID3 tag hierarchy provided"
<> header "Make your music library make sense" )
34 changes: 34 additions & 0 deletions src/Attributes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Attributes where

import Data.Accessor
import ID3.Simple
import ID3.Type

data Attribute = Artist
| Title
| Album
| Year
| Track
| Composer
deriving (Show, Eq)

toAttribute :: String -> Maybe Attribute
toAttribute "artist" = Just Artist
toAttribute "title" = Just Title
toAttribute "album" = Just Album
toAttribute "year" = Just Year
toAttribute "track" = Just Track
toAttribute "composer" = Just Composer
toAttribute _ = Nothing

getAttribute :: Tag -> Attribute -> Maybe String
getAttribute tag Artist = getArtist tag
getAttribute tag Title = getTitle tag
getAttribute tag Year = getYear tag
getAttribute tag Track = getTrack tag
getAttribute tag Composer = getFrameText "TCOM" tag

getFrameText :: FrameID -> Tag -> Maybe String
getFrameText frid tag = case tag^.frame frid of
Nothing -> Nothing
Just fr -> Just (fr^.textContent)
6 changes: 0 additions & 6 deletions src/Data/String/Strip.hs

This file was deleted.

1 change: 1 addition & 0 deletions src/Itunes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Itunes where
2 changes: 2 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ packages:
# (e.g., acme-missiles-0.3)
extra-deps: [
idiii-0.1.3.3
, optparse-applicative-0.14.3.0
, regex-compat-0.95.1
]

# Override default flag values for local packages and extra-deps
Expand Down
33 changes: 33 additions & 0 deletions test/ArgsSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module ArgsSpec where

import Test.Hspec
import Options.Applicative
import Attributes
import Args

spec = do
describe "parsing arguments" $ do
it "parses properly supplied short arguments" $ do
getParseResult (
execParserPure defaultPrefs app [ "-s"
, "/Users/shterrett/iTunesMusic"
, "-t"
, "/Users/shterrett/music"
, "-a"
, "composer,album,artist"
])
`shouldBe` (Just $ Args "/Users/shterrett/iTunesMusic"
"/Users/shterrett/music"
[Composer, Album, Artist])
it "parses properly supplied long arguments" $ do
getParseResult (
execParserPure defaultPrefs app [ "--source"
, "/Users/shterrett/iTunesMusic"
, "--target"
, "/Users/shterrett/music"
, "--attributes"
, "composer,album,artist"
])
`shouldBe` (Just $ Args "/Users/shterrett/iTunesMusic"
"/Users/shterrett/music"
[Composer, Album, Artist])
19 changes: 0 additions & 19 deletions test/Data/String/StripSpec.hs

This file was deleted.

0 comments on commit 08d69b5

Please sign in to comment.