how to combine the elements of a list representing the elements of a Bash command to a new list featuring the equals symbol
08:53 24 Feb 2015

I have a list like the following:

commandOptionsAndArguments = ['myBigTool', '--num-callers', '30', '--leak-check', 'full', '--tool', 'memcheck', '--suppressions', 'etc/valgrind-root.supp', '--suppressions', 'Gaudi.supp/Gaudi.supp', '--suppressions', 'oracleDB.supp', '--suppressions', 'valgrindRTT.supp', '--suppressions', 'root.supp/root.supp', '--mySpecialFlag', '$(which python)', '$(which athena.py)', 'athenaConf.pkl']

This list consists of elements of a Bash command featuring options and, if they have them, their respective arguments. I want to convert it to the following list:

commandOptions = ['myBigTool', '--num-callers=30', '--leak-check=full', '--tool=memcheck', '--suppressions=etc/valgrind-root.supp', '--suppressions=Gaudi.supp/Gaudi.supp', '--suppressions=oracleDB.supp', '--suppressions=valgrindRTT.supp', '--suppressions=root.supp/root.supp', '--mySpecialFlag', '$(which python)', '$(which athena.py)', 'athenaConf.pkl']

This new list consists of the options and, if they have them, their respective arguments combined using the equals sign into new elements. The next step would be either to execute this command using subprocess or to convert it to a string for use with os.system:

" ".join(commandOptions)
# 'myBigTool --num-callers=30 --leak-check=full --tool=memcheck --suppressions=etc/valgrind-root.supp --suppressions=Gaudi.supp/Gaudi.supp --suppressions=oracleDB.supp --suppressions=valgrindRTT.supp --suppressions=root.supp/root.supp --mySpecialFlag $(which python) $(which athena.py) athenaConf.pkl'

My question is: How can I change the list commandOptionsAndArguments to the list commandOptions in a robust way, detecting both options with arguments and without arguments appropriately?

(Also, if anyone has any better names for the lists mentioned, feel free to suggest them. I'm not sure of the words I should be using to describe the way of using the equals sign to specify the arguments of command options as opposed to using whitespace.)

python list arguments command