cmdargs
cmdargs copied to clipboard
sudo-like cli: a way to instruct `cmdargs` to not look for flags in the remaining arguments
I am working on a program that has sudo-like interface:
sudo ls
sudo ls -l
sudo -u joe ls -l /
Here's my current code:
#!/usr/bin/env stack
-- stack --resolver lts-9.20 script --package cmdargs
{-# LANGUAGE DeriveDataTypeable #-}
{-# OPTIONS_GHC -fno-cse #-}
module Main where
import System.Console.CmdArgs as Args
import System.IO
data Sudo = Sudo { user :: Maybe String
, group :: Maybe String
, command :: [String] }
deriving (Data, Show)
main :: IO ()
main = do
m <- cmdArgs Sudo { user = def
, group = def
, command = def &= typ "COMMAND" &= args }
print m
It works fine for commands without flags or when -- is used to mark the end of flags.
$ ./cmdargs.hs ls 1 2 3
Sudo {user = Nothing, group = Nothing, command = ["ls","1","2","3"]}
$ ./cmdargs.hs -- ls -l
Sudo {user = Nothing, group = Nothing, command = ["ls","-l"]}
$ ./cmdargs.hs -u joe -- ls -l
Sudo {user = Just "joe", group = Nothing, command = ["ls","-l"]}
However, it doesn't work when a sub-command has flags and when -- is not used:
$ ./cmdargs.hs ls -l
Unknown flag: -l
$ ./cmdargs.hs -u joe ls -l
Unknown flag: -l
Q: How to instruct cmdargs to not look for flags in the remaining arguments?
The user could write: ,/cmdargs.hs -u joe -- ls -l, which would take advantage of the fact that -- separates arguments from flags.
But currently there is no way to get that behaviour from cmdargs - although it was requested once before, so seems a plausible addition.
Yep, I have wanted this for years for hledger, which sometimes invokes separate executables with their own flags. Everybody trips up on the need for -- in this case. http://hledger.org/manual.html#command-options