While working on a project to automate certain processes of installation of an operating system, I’ve used pandas dataframes to organize certain data into tables and, with the help of third parties, been enabled to use those dataframes in application menus to submit data to functions. What I’m attempting to do right now is turn commandline data such as
cryptsetup --help
whose output is along the lines of
Help options:
-?, --help Show this help message
--usage Display brief usage
-V, --version Print package version
--active-name=STRING Override device autodetection of dm device to be reencrypted
--align-payload=SECTORS Align payload at <n> sector boundaries - for luksFormat
--allow-discards Allow discards (aka TRIM) requests for device
-q, --batch-mode Do not ask for confirmation
--cancel-deferred Cancel a previously set deferred device removal
-c, --cipher=STRING The cipher used to encrypt the disk (see /proc/crypto)`
into a Json readable format to implement it into a pandas data frame to be used as a menu. In other words, I need the json data’s format to be able to be utilized by JSON, to create a menu of listed options containing the shortened option, the longer option, and option information.
Turning output into Json was what I thought initially in solving the problem, and seems the best option; to find some means of writing some series of regex with sed and awk that could parse command line –help data to something json readable. I’ve also attempted to just use pandas read_csv e.g.
def cryptsetup_help():
process = subprocess.Popen(["cryptsetup --help"], shell=True, stdout=subprocess.PIPE)
process_list = process.communicate()[0]
process_list_output = process_list.decode('utf-8')
names = ['Help', 'options', ' ']
df = pd.read_csv(io.StringIO(process_list_output), header = 2, names=['Help', 'options:', ' '], usecols = ['Help', 'options:', ' '], nrows=100)
print(df)
however, this leads to the same inability with parsing. I can’t exactly match the comma or the ‘–‘ as it’s not listed for every applicable line. Any help would be appreciated. A feasible table could list just two columns or all three in whatever way possible. Kindly help with a solution.
5