◆ GUI で入力ダイアログが開いてそこにコピペして実行したら CUI ツールが動くようにしたい
◆ C# で作ってみたけど別環境の準備が面倒
◆ Windows で標準で動かせる VBScript だと入力ダイアログを簡単に作れる
  ◆ → 結構いい感じ

たまに使う CUI ツールがあるのですが 毎回コマンドプロンプトを起動して そのフォルダに行って コマンド名にオプションに入力値を書くのがけっこう面倒です
例を出すと 「wget で URL を入力してファイルをダウンロード」 を考えるとかなり近いです

固定オプションは bat ファイルで指定しておいて 毎回 bat ファイルに引数で入力値だけ渡せば良いという風にはできます
それでもコマンドプロンプト開いたり移動したりとか手間はあります

理想は 「GUI ツールでダブルクリックで起動して 入力コントロールにクリップボードのデータを貼り付けて OK ボタンを押せば完了」 というものです

C# で Window を作る

自分で作るしかないかな と思って C# で簡単に作ってみたものです

prompt

CUI ツールを C# のアプリケーションから呼び出すとそれ固定になってしまうので単純に 「GUI 入力で文字列を取得する」 だけのものにしました

[Program.cs]
using System;
using System.Windows.Forms;

namespace inputbox
{
static class Program
{
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var result = args.Length == 0 ? InputBox.show("", "InputBox")
: args.Length == 1 ? InputBox.show(args[0], "InputBox")
: args.Length == 2 ? InputBox.show(args[0], args[1])
: args.Length > 2 ? InputBox.show(args[0], args[1], args[2]);
Console.WriteLine(result);
}
}
}

[InputBox.cs]
using System;
using System.Windows.Forms;

namespace inputbox
{
public partial class InputBox : Form
{
public static string show(string message, string title = "InputBox", string default_text = "")
{
var input_box = new InputBox();
input_box.Text = title;
input_box.textBox1.Text = default_text;
input_box.label1.Text = message;
input_box.ShowDialog();

return input_box.input;
}

public string input;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;

public InputBox()
{
this.initializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.input = this.textBox1.Text;
this.Close();
}

private void button2_Click(object sender, EventArgs e)
{
this.input = null;
this.Close();
}

private void initializeComponent()
{
this.SuspendLayout();

this.button1 = new System.Windows.Forms.Button()
{
Name = "button1",
Location = new System.Drawing.Point(59, 13),
Size = new System.Drawing.Size(102, 30),
Text = "OK",
UseVisualStyleBackColor = true,
TabIndex = 0,
};
this.button1.Click += new System.EventHandler(this.button1_Click);

this.button2 = new System.Windows.Forms.Button()
{
Name = "button2",
Location = new System.Drawing.Point(230, 13),
Size = new System.Drawing.Size(102, 30),
Text = "キャンセル",
UseVisualStyleBackColor = true,
TabIndex = 1,
};
this.button2.Click += new System.EventHandler(this.button2_Click);

this.panel1 = new System.Windows.Forms.Panel()
{
Name = "panel1",
Location = new System.Drawing.Point(0, 146),
Size = new System.Drawing.Size(384, 55),
Margin = new System.Windows.Forms.Padding(4),
BackColor = System.Drawing.SystemColors.Control,
Controls = { this.button1, this.button2 },
Dock = System.Windows.Forms.DockStyle.Bottom,
TabIndex = 1,
};

this.label1 = new System.Windows.Forms.Label()
{
Name = "label1",
Location = new System.Drawing.Point(13, 39),
Size = new System.Drawing.Size(48, 17),
AutoSize = true,
Margin = new System.Windows.Forms.Padding(4, 0, 4, 0),
MaximumSize = new System.Drawing.Size(360, 0),
Text = "テキスト",
};

this.textBox1 = new System.Windows.Forms.TextBox()
{
Name = "textBox1",
Location = new System.Drawing.Point(13, 95),
Size = new System.Drawing.Size(358, 24),
Margin = new System.Windows.Forms.Padding(4),
BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle,
TabIndex = 0,
};

this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Window;
this.ClientSize = new System.Drawing.Size(384, 201);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.panel1);
this.Font = new System.Drawing.Font("Meiryo UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.Margin = new System.Windows.Forms.Padding(4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.ShowIcon = false;
this.Text = "Title";

this.ResumeLayout(false);
this.PerformLayout();
}
}
}

これを console app としてビルドします
起動の速さのため WinForms を使っていて GUI のレイアウト系もコードに含まれています
そのせいで長いですが 中身は引数で受け取ったメッセージを 本文・タイトル・入力デフォルト値 に設定して表示するだけものです

結果は標準出力に出力されます

bat ファイルで

for /f %%i in ('inputbox "URL を入力してください"') do set url=%%i
wget %url%
pause

これを実行すれば 入力したものを wget でダウンロードできます
Windows 標準では wget コマンドはないので ↑ をそのまま動かすには wget をインストール必要です

VBScript

とりあえずできたのですが 別環境でも使おうとすると CUI のツールに加えて 作った exe と bat ファイルまで必要で準備が面倒です

考えてみると alert のようなものなら JScript でも表示できました
Windows 標準ツールで出せるんじゃないの? と思って調べてみると VBScript でできるみたいです

VBScript でできるなら JScript でもできる? と思ったのですが VBScript のみみたいです
VBScript でダイアログを出して結果を返す関数を作って JScript からその関数を呼び出すことはできるようなのですが 今回のはたいしたコードの長さでもないので最初から VBScript にしました

Dim command, shell, url

command = "cuitool.exe -f ""option"" --option1 --option2 ""optionoptionoption"""
url = InputBox("URL")

IF Len(Trim(url)) Then
Set shell = WScript.CreateObject("WScript.Shell")
shell.Run "cmd.exe /C " & command & " " & url & "& pause"
End If

VBScript では Input のダイアログは InputBox 関数を呼び出すだけで使えます
command 変数に wget など実行するコマンドを入れます

入力データがあったら cmd /C を使ってコマンドプロンプトで実行します
すぐに閉じないように 最後に pause して結果を見れるようにしています

たったこれだけで GUI ダイアログで入力して CUI ツールを動かせるのですから VBScript も捨てたものではないですね
見た目はイマイチですけど

vbdialog

特に見た目を気にする部分でもないですしね