sendkeys在github api(命令行)中没有工作

发布于 2025-01-23 07:35:05 字数 1019 浏览 0 评论 0原文

我正在尝试使用PowerShell和GH Repo Create自动创建远程存储库,并且运行该命令后发生的第一件事是在GitHub上创建新仓库或推动现有的本地回购的选择向上。我想选择前者,这应该只需要击中输入,因为这是默认情况下突出显示的选项。我正在尝试在PS1脚本中使用它:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}");

当我在没有gh repo create命令的情况下尝试时,它可以按预期工作,在PowerShell控制台中创建新行。但是,当遵循gh repo创建时,它似乎无能为力。 文本

What would you like to do?  [Use arrows to move, type to filter]
> Create a new repository on GitHub from scratch
  Push an existing local repository to GitHub

控制台

gh repo create
[Microsoft.VisualBasic.Interaction]::AppActivate("Administrator: Microsoft Powershell")
Add-Type -AssemblyName System.Windows.Forms
Start-Sleep 3
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

仅位于以下

$wshell = New-Object -ComObject wscript.shell;
$wshell.SendKeys("{ENTER}")

上做错了什么,或者,如果出于某种原因而sendkey只是不使用GH命令,则似乎是后者。任何想法都将不胜感激。

I'm trying to automate the creating of remote repos using powershell and gh repo create and the first thing that happens after running that command is an option to create a new repo on GitHub or push an existing local repo up. I want to select the former, which should just require hitting enter since this is the option that is highlighted by default. I'm trying to use this in my ps1 script:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}");

When I try that without the gh repo create command, it works as expected, creating a new line in the powershell console. But when following gh repo create, it appears to do nothing. The console just sits on the following text which is output from the gh repo create command:

What would you like to do?  [Use arrows to move, type to filter]
> Create a new repository on GitHub from scratch
  Push an existing local repository to GitHub

I have tried countless combinations of the following commands

gh repo create
[Microsoft.VisualBasic.Interaction]::AppActivate("Administrator: Microsoft Powershell")
Add-Type -AssemblyName System.Windows.Forms
Start-Sleep 3
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

and

$wshell = New-Object -ComObject wscript.shell;
$wshell.SendKeys("{ENTER}")

I'm new to powershell and can't tell if I'm doing something wrong or if sendkeys just doesn't work with gh commands for some reason, seems to be the latter. Any ideas would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝情姑娘 2025-01-30 07:35:05

扩展 @McLayton的注释...

sendkeys是为与多线程执行环境相似的方案而设计的一个不同的线程,您可以在脚本中使用sendkeys将击键发送到UI。

正如@mclayton指出的那样,问题是该控制台应用程序的行为更像是单线程的环境:因此,在这种情况下,gh命令在其之后阻止了所有内容。

如果要走此路线,请尝试将sendkeys的输出输送到gh命令,类似于

[System.Windows.Forms.Sendkeys] :: SendWait (“ {enter}”)| GH repo创建

我不确定如何工作(据我所知,Pipes在PowerShell中的行为与常规命令行接口有所不同)。
您甚至可能不需要使用sendkeys;您可能只能使用写入主持人或类似方法。

请注意,这是管道的原始用例:能够将一个命令的输出发送到另一个命令IE command1 [options] | command2 [options] ...,因此即使在“单线线程”命令行界面中也能够在程序之间进行通信。

Expanding on @mclayton's comments ...

SendKeys is designed for scenarios that are similar to a multithreaded execution environment: you've got some bit of UI waiting for input on one thread but also have a script running on a different thread, and you can use SendKeys in the script to send keystrokes to the UI.

The problem is, as @mclayton points out, that console applications tend to behave more like a single-threaded environment: so, in this case, the gh command is blocking everything after it.

If you want to go this route, try piping the output of SendKeys to the gh command, something like

[System.Windows.Forms.SendKeys]::SendWait("{ENTER}") | gh repo create

I'm not exactly sure how that would work (as I understand it, pipes behave somewhat differently in PowerShell than in the regular command line interface).
You might not even need to use SendKeys; you might just be able to use Write-Host or similar.

Note that this was the original use case for pipes: to be able to send the output of one command to another i.e. command1 [options] | command2 [options] ... and therefore be able to communicate between programs, even in a "single-threaded" command-line interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文