-
Notifications
You must be signed in to change notification settings - Fork 1
/
apavecli.tcl
executable file
·96 lines (87 loc) · 2.97 KB
/
apavecli.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#! /usr/bin/env tclsh
############################################################################
#
# Runs pave dialogs from CLI.
# Scripted by Alex Plotnikov.
#
# After choosing 'OK' in a dialog, the dialog's result is written to stdout.
# The output would be sort of:
# #!/bin/bash
# export var1='value 1'
# export var2='value 2'
# ...
# export varN='value 3'
# The output may be redirected to temp file. After analizing the result of
# dialog (1 if 'OK', otherwise 0), the output temp file may be sourced to
# execute the "export ..." commands. So that the dialog's variable values
# would be assigned to the environment variables with appropriate names,
# within the current shell.
#
# Example:
#
# tclsh ~/UTILS/pave/pavecli.tcl "" "TEST OF pavecli" \
{ ent1 {" Find: "} {"$::EN1 2 3"} ent2 {"Replace: "} \
{"$::EN2 $::EN4"} labo {{} {-anchor w} {-t "\\nOptions:" -font \
{-weight bold}}} {} radA {{Match: }} {{RE } Exact "Glob" \
{RE }} seh {{} {} {}} {} chb1 {{Match whole word only}} \
{1} chb2 {{Match case }} {1} seh2 {{} {} {}} {} \
v_ {{} {} {}} {} cbx1 {{Where: }} {{"in file"} {in file} \
{in session} {in directory}} } -head "Enter data:" -weight bold \
== EN1 EN2 V1 C1 C2 W1 > tmp.sh ; \
if [ $? -eq 1 ]; then source tmp.sh; fi ; \
rm tmp.sh ; \
echo "EN1=$EN1, EN2=$EN2, V1=$V1, C1=$C1, C2=$C2, W1=$W1"
#
# The end of command looks like this:
# == EN1 EN2 V1 C1 C2 W1 > tmp.sh
# which sets the EN1, EN2, V1, C1, C2 and W1 output variables and redirects
# their assignment to tmp.sh file. After checking the command's result, the
# tmp.sh file is executed in the current shell, by 'source' command.
#
# So the EN1, EN2, V1, C1, C2 and W1 environment variables would correspond
# to the dialog's variables.
#
# See also:
# https://aplsimple.github.io/en/tcl/pave
#
############################################################################
if {[catch {package require apave}]} {
set ::apavedir [file dirname [info script]]
lappend auto_path $::apavedir
if {[catch {package require apave}]} {
lset auto_path end $::apavedir/pave
package require apave
}
}
namespace eval apavecli {
}
#=== Input dialog for getting data
proc ::apavecli::input {args} {
::apave::APave create dialog
set cmd [subst -nocommands -novariables [string range $args 1 end-1]]
set dp [string last " ==" $cmd]
if {$dp<0} {set dp 999999}
set data [string range $cmd $dp+3 end]
set data [split [string trim $data]]
set cmd "dialog input [string range $cmd 0 $dp-1]"
set res [eval $cmd]
set r [lindex $res 0]
if {$r && $data ne ""} {
set rind 0
puts "#!/bin/bash"
foreach res [lrange $res 1 end] {
puts "export [lindex $data $rind]='$res'"
incr rind
}
}
return $r
}
#=== Run dialog
proc ::apavecli::run {} {
apave::initWM
set res [::apavecli::input $::argv]
::apave::APave destroy
exit $res
}
::apavecli::run
#% DOCTEST SOURCE tests/apavecli.test