Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rapidly develop GUI for command line [closed]

I am looking for a software which would help me quickly build GUI (eg. HTML pages). When selecting different option from the GUI (eg. HTML forms), those options would magically turn into command line options. Eg: my GUI would allow me to select:

  • Select input filename: inputfn
  • Select output filename: outputfn
  • Set Option1 to ON
  • Set Option2 to OFF
  • Set Option3 to value '42'
  • ...

When clicking on 'Submit', this user input would turn into something like:

$ cmd_line --input $inputfn --output $outputfn --option1-on --option2-off --option3=42

I imagine I would need to write up a simple XML (YAML?) configuration file, to describe the simple layout and command line specificity (radio button, text fields ...).

Am I dreaming or is there something like this available ?

like image 461
malat Avatar asked Dec 30 '13 15:12

malat


People also ask

Is command line faster than GUI?

CLI is faster than GUI. The speed of GUI is slower than CLI. 5. CLI operating system needs only a keyboard.

Is command line more efficient than GUI?

GUIs offer better multitasking and control Being more user-friendly than a command line (especially for new or novice users), a visual file system is utilized by more people. GUI users have windows that enable a user to view, control, manipulate, and toggle through multiple programs and folders at same time.

Why is the GUI interface easier to use than the CLI?

GUI is easier to learn and use. This is because of its user-friendly interface. Users receive immediate visual feedback when dealing with GUI, while this is not obvious in CLI. It would help if you had a higher degree of memorization and familiarity to effectively navigate and operate devices powered by CLI.


1 Answers

This post seems to ask almost exactly what you want.

The top solution of using YAD seems to cover your requisites.

For example the following one liner:

data="$(yad --title="Desktop entry editor" --text="Simple desktop entry editor" --form --field="Type:CB" --field="Name" --field="Generic name" --field="Comment" --field="Command:FL" --field="Icon" --field="In terminal:CHK" --field="Startup notify:CHK" "Application" "Name" "Generic name" "This is the comment" "/usr/bin/yad" "yad" FALSE TRUE --button="WebUpd8:2" --button="gtk-ok:0" --button="gtk-cancel:1")"

creates a dialog containing:

  • text fields
  • dropdown selections
  • checkboxes
  • buttons

that looks like:

enter image description here

The output is all put on yad stdout and echo "$data" looks something like:

Application|Name|Generic name|This is the comment|/usr/bin/yad|yad|FALSE|TRUE|

Now you can "parse" the output with some cmdline tool such as cut or awk:

a="$(echo "$data" | cut -d"|" -f1)"
b="$(echo "$data" | cut -d"|" -f1)"

Then there is just application logic left to you.

Ubuntu install:

sudo add-apt-repository ppa:webupd8team/y-ppa-manager
sudo apt-get update
sudo apt-get install yad

EDIT: focusing on the convertion of GUI to command line options

After receiving a downvote, I think I interpreted the answer the wrong way. The key point he wants is conversion from GUI to cmdline options interface.

I do not know of an existing solution, and since after 7 bounty days still no answer, we can assume that there is no existing solution.

Therefore, the best we can do is help the OP create a solution himself. I believe that since there are not that many command line option cases, a reasonably small script will be sufficient.

Here goes a Python + Tk example

#!/usr/bin/env python

import Tkinter
import tkFileDialog

class Option(object):
    def __init__(self, optype, name, widget=Tkinter.Entry):
        self.optype = optype
        self.name = name
        if self.optype == 'switch':
            self.widget = Tkinter.Checkbutton
        else:
            self.widget = widget

## INPUT -------------------------------------------------------
options = [
    Option("key-value", "--text="),
    Option("switch", "-s"),
    Option("positional", "pos0"),
    Option("positional", "pos1", tkFileDialog.askopenfilename),
]
## END -------------------------------------------------------

def askopenfilename(filename):
    filename.set(tkFileDialog.askopenfilename())

# Build command
def ok_func():
    cmd = "cmd_line"
    for option in options:
        if option.optype == "key-value":
            val = tkvars[option.name].get()
            if val:
                cmd += ' '
                cmd += '%s"%s"' % (option.name, val)
        elif option.optype == "switch":
            cmd += ' '
            if tkvars[option.name].get() == 1:
                cmd += option.name
        elif option.optype == "positional":
            val = tkvars[option.name].get()
            if val:
                cmd += ' '
                cmd += val
    print cmd


tkvars = {}
app = Tkinter.Tk()
grid = Tkinter.Frame()
row = 0

# Build GUI
for option in options:
    Tkinter.Label(grid, text=option.name).grid(row=row, column=0, sticky='W')
    if option.widget == Tkinter.Entry:
        tkvars[option.name] = Tkinter.StringVar()
        widget = Tkinter.Entry(
            grid,
            textvariable=tkvars[option.name]
        )
    elif option.widget == Tkinter.Checkbutton:
        tkvars[option.name] = Tkinter.IntVar()
        widget = Tkinter.Checkbutton(
            grid,
            variable = tkvars[option.name],
            onvalue = 1
        )
    elif option.widget == tkFileDialog.askopenfilename:
        tkvars[option.name] = Tkinter.StringVar()
        widget = Tkinter.Button(
            grid,
            text='Browse',
            command=lambda: askopenfilename(tkvars[option.name])
        )
    widget.grid(row=row, column=1, sticky='W')
    row += 1

grid.pack()
Tkinter.Button(app, text="ok", command=ok_func).pack()
Tkinter.Button(app, text="quit", command=app.quit).pack()
app.mainloop()

To run on Ubuntu 12.04 first do:

sudo apt-get install python-tk

Screenshot:

enter image description here

Command generated for the screenshot:

cmd_line --text="asdf" -s qwer /path/to/file

Behaviour:

  • if the value of --text= is empty then it is omitted
  • switches only appear if the corresponding checkbox is checked

If you want to implement a new feature on top of that code, I recommend you do it here. If it gets good enough, lets split to a new repo and make it pip installable.