use Alternative for parsing KeyOptions
This commit is contained in:
		
					parent
					
						
							
								94e703e8b8
							
						
					
				
			
			
				commit
				
					
						032e6485fa
					
				
			
		
					 3 changed files with 38 additions and 51 deletions
				
			
		|  | @ -55,46 +55,37 @@ gitAnnexOptions = commonOptions ++ | ||||||
| 		>>= Annex.changeGitRepo | 		>>= Annex.changeGitRepo | ||||||
| 
 | 
 | ||||||
| -- Options for acting on keys, rather than work tree files. | -- Options for acting on keys, rather than work tree files. | ||||||
| data KeyOptions = KeyOptions | data KeyOptions | ||||||
| 	{ wantAllKeys :: Bool | 	= WantAllKeys | ||||||
| 	, wantUnusedKeys :: Bool | 	| WantUnusedKeys | ||||||
| 	, wantIncompleteKeys :: Bool | 	| WantSpecificKey Key | ||||||
| 	, wantSpecificKey :: Maybe Key | 	| WantIncompleteKeys | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| parseKeyOptions :: Bool -> Parser KeyOptions | parseKeyOptions :: Bool -> Parser KeyOptions | ||||||
| parseKeyOptions allowincomplete = KeyOptions | parseKeyOptions allowincomplete = if allowincomplete | ||||||
| 	<$> parseAllKeysOption | 	then base | ||||||
| 	<*> parseUnusedKeysOption | 		<|> flag' WantIncompleteKeys | ||||||
| 	<*> (if allowincomplete then parseIncompleteOption else pure False) |  | ||||||
| 	<*> parseSpecificKeyOption |  | ||||||
| 
 |  | ||||||
| parseAllKeysOption :: Parser Bool |  | ||||||
| parseAllKeysOption = switch |  | ||||||
| 	( long "all" <> short 'A' |  | ||||||
| 	<> help "operate on all versions of all files" |  | ||||||
| 	) |  | ||||||
| 
 |  | ||||||
| parseUnusedKeysOption :: Parser Bool |  | ||||||
| parseUnusedKeysOption = switch |  | ||||||
| 	( long "unused" <> short 'U' |  | ||||||
| 	<> help "operate on files found by last run of git-annex unused" |  | ||||||
| 	) |  | ||||||
| 
 |  | ||||||
| parseSpecificKeyOption :: Parser (Maybe Key) |  | ||||||
| parseSpecificKeyOption = optional $ option (str >>= parseKey) |  | ||||||
| 	( long "key" <> metavar paramKey |  | ||||||
| 	<> help "operate on specified key" |  | ||||||
| 	) |  | ||||||
| 
 |  | ||||||
| parseKey :: Monad m => String -> m Key |  | ||||||
| parseKey = maybe (fail "invalid key") return . file2key |  | ||||||
| 
 |  | ||||||
| parseIncompleteOption :: Parser Bool |  | ||||||
| parseIncompleteOption = switch |  | ||||||
| 			( long "incomplete" | 			( long "incomplete" | ||||||
| 			<> help "resume previous downloads" | 			<> help "resume previous downloads" | ||||||
| 			) | 			) | ||||||
|  | 	else base | ||||||
|  |   where | ||||||
|  | 	base = | ||||||
|  | 		flag' WantAllKeys | ||||||
|  | 			( long "all" <> short 'A' | ||||||
|  | 			<> help "operate on all versions of all files" | ||||||
|  | 			) | ||||||
|  | 		<|> flag' WantUnusedKeys | ||||||
|  | 			( long "unused" <> short 'U' | ||||||
|  | 			<> help "operate on files found by last run of git-annex unused" | ||||||
|  | 			) | ||||||
|  | 		<|> (WantSpecificKey <$> option (str >>= parseKey) | ||||||
|  | 			( long "key" <> metavar paramKey | ||||||
|  | 			<> help "operate on specified key" | ||||||
|  | 			)) | ||||||
|  | 
 | ||||||
|  | parseKey :: Monad m => String -> m Key | ||||||
|  | parseKey = maybe (fail "invalid key") return . file2key | ||||||
| 
 | 
 | ||||||
| -- Options to match properties of annexed files. | -- Options to match properties of annexed files. | ||||||
| annexedMatchingOptions :: [Option] | annexedMatchingOptions :: [Option] | ||||||
|  |  | ||||||
|  | @ -172,7 +172,7 @@ withNothing _ _ = error "This command takes no parameters." | ||||||
|  - |  - | ||||||
|  - Otherwise falls back to a regular CommandSeek action on |  - Otherwise falls back to a regular CommandSeek action on | ||||||
|  - whatever params were passed. -} |  - whatever params were passed. -} | ||||||
| withKeyOptions :: KeyOptions -> Bool -> (Key -> CommandStart) -> (CmdParams -> CommandSeek) -> CmdParams -> CommandSeek | withKeyOptions :: Maybe KeyOptions -> Bool -> (Key -> CommandStart) -> (CmdParams -> CommandSeek) -> CmdParams -> CommandSeek | ||||||
| withKeyOptions ko auto keyaction = withKeyOptions' ko auto $ \getkeys -> do | withKeyOptions ko auto keyaction = withKeyOptions' ko auto $ \getkeys -> do | ||||||
| 	matcher <- Limit.getMatcher | 	matcher <- Limit.getMatcher | ||||||
| 	seekActions $ map (process matcher) <$> getkeys | 	seekActions $ map (process matcher) <$> getkeys | ||||||
|  | @ -182,25 +182,21 @@ withKeyOptions ko auto keyaction = withKeyOptions' ko auto $ \getkeys -> do | ||||||
| 		, return Nothing | 		, return Nothing | ||||||
| 		) | 		) | ||||||
| 
 | 
 | ||||||
| withKeyOptions' :: KeyOptions -> Bool -> (Annex [Key] -> Annex ()) -> (CmdParams -> CommandSeek) -> CmdParams -> CommandSeek | withKeyOptions' :: Maybe KeyOptions -> Bool -> (Annex [Key] -> Annex ()) -> (CmdParams -> CommandSeek) -> CmdParams -> CommandSeek | ||||||
| withKeyOptions' ko auto keyaction fallbackaction params = do | withKeyOptions' ko auto keyaction fallbackaction params = do | ||||||
| 	bare <- fromRepo Git.repoIsLocalBare | 	bare <- fromRepo Git.repoIsLocalBare | ||||||
| 	let allkeys = wantAllKeys ko |  | ||||||
| 	let unused = wantUnusedKeys ko |  | ||||||
| 	let incomplete = wantIncompleteKeys ko |  | ||||||
| 	let specifickey = wantSpecificKey ko |  | ||||||
| 	when (auto && bare) $ | 	when (auto && bare) $ | ||||||
| 		error "Cannot use --auto in a bare repository" | 		error "Cannot use --auto in a bare repository" | ||||||
| 	case	(allkeys, unused, incomplete, null params, specifickey) of | 	case (null params, ko) of | ||||||
| 		(False  , False , False     , True       , Nothing) | 		(True, Nothing) | ||||||
| 			| bare -> go auto loggedKeys | 			| bare -> go auto loggedKeys | ||||||
| 			| otherwise -> fallbackaction params | 			| otherwise -> fallbackaction params | ||||||
| 		(False  , False , False     , _          , Nothing) -> fallbackaction params | 		(False, Nothing) -> fallbackaction params | ||||||
| 		(True   , False , False     , True       , Nothing) -> go auto loggedKeys | 		(True, Just WantAllKeys) -> go auto loggedKeys | ||||||
| 		(False  , True  , False     , True       , Nothing) -> go auto unusedKeys' | 		(True, Just WantUnusedKeys) -> go auto unusedKeys' | ||||||
| 		(False  , False , True      , True       , Nothing) -> go auto incompletekeys | 		(True, Just (WantSpecificKey k)) -> go auto $ return [k] | ||||||
| 		(False  , False , False     , True       , Just k) -> go auto $ return [k] | 		(True, Just WantIncompleteKeys) -> go auto incompletekeys | ||||||
| 		_ -> error "Can only specify one of file names, --all, --unused, --key, or --incomplete" | 		(False, Just _) -> error "Can only specify one of file names, --all, --unused, --key, or --incomplete" | ||||||
|   where |   where | ||||||
| 	go True _ = error "Cannot use --auto with --all or --unused or --key or --incomplete" | 	go True _ = error "Cannot use --auto with --all or --unused or --key or --incomplete" | ||||||
| 	go False getkeys = keyaction getkeys | 	go False getkeys = keyaction getkeys | ||||||
|  |  | ||||||
|  | @ -51,7 +51,7 @@ data FsckOptions = FsckOptions | ||||||
| 	{ fsckFiles :: CmdParams | 	{ fsckFiles :: CmdParams | ||||||
| 	, fsckFromOption :: Maybe RemoteName | 	, fsckFromOption :: Maybe RemoteName | ||||||
| 	, incrementalOpt :: Maybe IncrementalOpt | 	, incrementalOpt :: Maybe IncrementalOpt | ||||||
| 	, keyOptions :: KeyOptions | 	, keyOptions :: Maybe KeyOptions | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| data IncrementalOpt | data IncrementalOpt | ||||||
|  | @ -67,7 +67,7 @@ optParser desc = FsckOptions | ||||||
| 		<> help "check remote" | 		<> help "check remote" | ||||||
| 		)) | 		)) | ||||||
| 	<*> optional parseincremental | 	<*> optional parseincremental | ||||||
| 	<*> parseKeyOptions False | 	<*> optional (parseKeyOptions False) | ||||||
|   where |   where | ||||||
| 	parseincremental = | 	parseincremental = | ||||||
| 		flag' StartIncrementalO | 		flag' StartIncrementalO | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Joey Hess
				Joey Hess