解析命令行参数 [英] Parsing command line arguments
问题描述
我对 prolog 很困惑,它与我曾经使用过的任何语言(多种语言)都不一样,我如何从以下位置获取 argv[0]:
I'm very confused with prolog, it's way different to any language I've ever used (many languages) How do I go about getting argv[0] from:
current_prolog_flag(argv, Argv),
write(Argv).
现在,如果我尝试输入 Argv[0] 或 Argv(0) 或 Argv<0> 它会失败.. 这让我没有任何线索,文档中的帮助也很少.. 似乎他们希望你已经成为序言专家 :D
Now if I tried to type Argv[0] or Argv(0) or Argv<0> it fails.. this leaves me with no clue and very little help from the documentation.. it seems that they expect you to already be a prolog expert :D
另一个问题,我如何将 Argv[0] 分配给一个变量,以便稍后使用write"打印它?
Another question, how would I assign Argv[0] to a variable so I can print it later using "write" ?
推荐答案
Prolog 使用匹配.
Prolog uses matching.
?- current_prolog_flag(argv, [File | Rest]).
File = 'C:\\Program Files\\pl\\bin\\swipl-win.exe',
Rest = ['--win_app'].
这匹配一个有头有尾的列表:
This matches a list with a head and the tail:
[Head | Tail]
Head 是第一个元素,Tail 是列表的其余部分.
Head is the first element and Tail is the rest of the list.
要获取最后一个元素,请使用:
To get the last element, use:
?- current_prolog_flag(argv, Argv), append(_, [Last], Argv).
Argv = ['C:\\Program Files\\pl\\bin\\swipl-win.exe', '--win_app'],
Last = '--win_app'
要获取有关 append 等函数的帮助:
To get help about functions like append:
apropos(append).
这篇关于解析命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!