首页 \ 问答 \ Windows命令行(Windows Command Line)

Windows命令行(Windows Command Line)

当有人直接输入到windows命令行时,是否有人知道如何摆脱for循环? 我知道你可以使用gotos和标签在批处理文件中突破它,但我找不到任何关于在命令行中突破一个的东西。 这是一个简单的例子:

C:> for /l %i in (1,0,1) do @ping -n 1 google.com || (echo ^G & msg user "Google is down!" & QUIT)

这应该无限ping google.com。 如果它失败了,它会发出嘟嘟声(echo ^ G),向用户“user”显示一个消息框,上面写着“Google已关闭!”和QUIT。 我不知道如何做退出部分。 我想我可以做像taskkill / f / im cmd.exe这样的东西,但我一直在寻找更优雅的东西。 有小费吗?


Does anyone know how to break out of a for loop when it's typed directly into the windows command-line? I know you can use gotos and labels to break out of it when it's in a batch file, but I can't find anything about breaking out of one on the command line. Here's a simple example:

C:> for /l %i in (1,0,1) do @ping -n 1 google.com || (echo ^G & msg user "Google is down!" & QUIT)

This should infinitely ping google.com. If it ever fails, it beeps (echo ^G), displays a message box to the user "user" that says "Google is down!", and QUITs. I don't know how to do the quit part though. I guess I could do something like taskkill /f /im cmd.exe, but I was looking for something more elegant. Any tips?


原文:https://stackoverflow.com/questions/2437071
更新时间:2023-06-07 13:06

最满意答案

由于discord.py的事件驱动模型,这实际上比听起来更棘手。 我们的命令将接受我们的查询的一些参数,并返回给用户他们选择的逐项列表。 我们将该用户和该列表存储在字典中。 然后,在我们的on_message事件中,我们将针对我们关心的作者的字典检查每条消息,并尝试将其消息解释为列表中的选项,直到它们进行有效选择。

waiting_for = {}

@bot.command(pass_context=True)
async def fort(ctx, arg):
    # database setup stuff
    names = cursor.execute("SELECT name FROM forts WHERE name LIKE '" + str(arg) + "%';")
    if not names:
        await bot.say("That fort does not exist")
    elif len(names) == 1:
        await process_fort(name)
    else:
        choices = "\n".join("{}. {}".format(i, x) for i, x in enumerate(names, start=1))
        await bot.say("Your choices are")
        await bot.say(choices)
        waiting_for[ctx.message.author.id] = names


@bot.event
async def on_message(message):
    id = message.author.id
    if id in waiting_for:
        if message.content.isdigit():
            names = waiting_for[id]
            selection = int(message.content)
            if 0 < selection < len(names):
                del waiting_for[id]
                await process_fort(name)
                return
    await bot.process_commands(message)

# Whatever you want to do with the name once you have it
async def process_fort(name):
    ...

如果这看起来很复杂,您也可以向用户回显所有可能性,直到输入仅返回一个结果的查询。


This is actually trickier than it sounds, because of discord.py's event driven model. Our command will accept some argument to our query, and return to the user an itemized list of their choices. We'll store that user and that list in a dictionary. Then, in our on_message event we'll check each message against the dict of authors we care about, and try to interpret their messages as choices in the list until they make a valid selection.

waiting_for = {}

@bot.command(pass_context=True)
async def fort(ctx, arg):
    # database setup stuff
    names = cursor.execute("SELECT name FROM forts WHERE name LIKE '" + str(arg) + "%';")
    if not names:
        await bot.say("That fort does not exist")
    elif len(names) == 1:
        await process_fort(name)
    else:
        choices = "\n".join("{}. {}".format(i, x) for i, x in enumerate(names, start=1))
        await bot.say("Your choices are")
        await bot.say(choices)
        waiting_for[ctx.message.author.id] = names


@bot.event
async def on_message(message):
    id = message.author.id
    if id in waiting_for:
        if message.content.isdigit():
            names = waiting_for[id]
            selection = int(message.content)
            if 0 < selection < len(names):
                del waiting_for[id]
                await process_fort(name)
                return
    await bot.process_commands(message)

# Whatever you want to do with the name once you have it
async def process_fort(name):
    ...

If that seems to complicated, you could also just echo all the possibilities to the user until the enter a query that only returns one result.

相关问答

更多
  • 您的ide可能使用了与您下载discord.py的不同的python解释器。 您需要更改sdk设置。 请遵循以下教程: https : //www.jetbrains.com/help/idea/2017.1/configuring-python-interpreter-for-a-project.html 要为Python模块配置Python解释器,请按照下列步骤操作 打开“项目结构”对话框(例如Ctrl + Shift + Alt + S)。 在“项目结构”对话框中,单击“项目设置”下的“模块”节点。 ...
  • 要回答你的第一个问题,是的,你可以在一个单独的文件中写入命令,并让它们仍然注册为main。 在discord.py中,这被称为使用齿轮。 这是一个例子 。 假设这就是你的代码,你的代码有几个问题。 首先,你必须总是await协程函数才能使用它们 - > await client.send_message("Hello") ... await client.say("Hello")其次,我不确定创建一个“userhandler”是一个好主意。 特别是你试图考虑的方式有更简单的方法来获取用户对象。 最后,请参阅 ...
  • 在Windows上,python可执行文件的正常名称是python.exe (控制台程序)或pythonw.exe (用于GUI程序)。 python可执行文件有时在某些平台上称为python3 ,其中默认( python )是旧的python 2.在许多基于UNIX(包括Linux和OS X)的系统上,系统实用程序使用python 2,更改它可能有这些平台上的不良后果,因此名称为“python3”。 在Windows上你应该没问题 - 在Windows上还有其他问题,除非你尝试使用多个python版本,否 ...
  • 由于您正在使用discord api,如果您阅读了client.send_message的描述,如果您在embed发送的消息超过2000个chrs,则discord将引发400请求错误。 对于Discord,角色限制为2000。 正如您所看到的,它实际上并不是真正的错误, discord.errors.HTTPException: BAD REQUEST (status code: 400) 。 这是discord API的自定义错误。 要更正它,您可以将消息拆分为小于2000个chrs的嵌入并单独发送。 ...
  • 对于异步只是做 await client.delete_message(message) 否则重写只是做 await message.delete() for async just do await client.delete_message(message) otherwise for rewrite just do await message.delete()
  • 由于discord.py的事件驱动模型,这实际上比听起来更棘手。 我们的命令将接受我们的查询的一些参数,并返回给用户他们选择的逐项列表。 我们将该用户和该列表存储在字典中。 然后,在我们的on_message事件中,我们将针对我们关心的作者的字典检查每条消息,并尝试将其消息解释为列表中的选项,直到它们进行有效选择。 waiting_for = {} @bot.command(pass_context=True) async def fort(ctx, arg): # database setup ...
  • bot.say是一个协程 ,所以你必须yield from或await结果。 另外,你不能在命令之外使用bot.say 你应该尝试从控制机器人的代码中分离出游戏运行的所有代码。 bot.say is a coroutine, so you must either yield from or await the result. Additionally, you cannot use bot.say outside of commands You should try to separate all of t ...
  • delete_channel('kick')将无法工作,因为您需要传入channel对象而不是字符串。 您不需要使用discord.utils来获取所需的频道。 create_channel返回一个通道对象,因此您应该可以使用它。 但是,你确实需要获得你要踢的Member对象。 您还错误地引用了message.server而不是ctx.message.server @bot.command(pass_context=True) async def kick(ctx, victim): victim_ ...
  • 您可以运行aiohttp以及任何使用asyncio的库。 或者你可以在单独的线程中启动aiohttp。 I've found a solution of sorts, I had to use an async web server, namely the poorly documented Kyoukai - http://kyoukai.readthedocs.io/en/latest/ I can now silmeoutaneously interface with the Discord API ...
  • 您的问题似乎主要是关于混合同步和异步代码。 有两种可能性: 1)如果你的非异步例程不需要阻塞,只需要调度一些异步任务(例如send_message )稍后运行,那么他们可以简单地调用get_event_loop().create_task() 。 如果您希望在异步操作完成时调用其他(非异步)例程,您甚至可以在返回的任务上使用add_done_callback 。 (如果要运行的例程也是非异步的,则使用get_event_loop().call_soon() 。) 2)如果你的非异步例程绝对必须阻塞(其中包括 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)