Command line 如何从cmd运行程序
我希望能够在任何文本文件上运行它,而不必在代码中指定路径。有人能帮忙吗?谢谢Command line 如何从cmd运行程序,command-line,f#,arguments,command-line-arguments,Command Line,F#,Arguments,Command Line Arguments,我希望能够在任何文本文件上运行它,而不必在代码中指定路径。有人能帮忙吗?谢谢 open System.IO type Data() = member x.Read() = use stream = new StreamReader @"D:\Searcher.txt" let mutable valid = true printfn "Please enter the number you want to search for: "
open System.IO
type Data() =
member x.Read() =
use stream = new StreamReader @"D:\Searcher.txt"
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
let data = Data()
data.Read()
System.Console.ReadKey() |> ignore
opensystem.IO
类型数据()=
成员十读()=
使用stream=newstreamreader@“D:\Searcher.txt”
设可变有效=true
printfn“请输入要搜索的号码:”
让mutable x=System.Console.ReadLine()
设可变线=1
虽然(有效)做什么
let line=stream.ReadLine()
如果(line=null),则
valid将文件名作为参数传递有三种方法。在编译的程序中使用argv
,如果通过fsi执行,则使用fsi.CommandLineArgs
,最后通过Console.Readline
获取文件名参数,与获取要搜索的字符串完全相同。你不需要定义一个类来做这件事,所以我删除了它,把它变成了一个函数。您还可以简化read函数中的代码,使其不那么必要
第1版
在编译程序中,argv
作为字符串列表传递给main:
open System.IO
[<EntryPoint>]
let main argv =
let fName = argv.[0] // get the file name as the first argument
let readData (fName:string) =
use stream = new StreamReader(fName)
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
readData fName
System.Console.ReadKey() |> ignore
0 // return an integer exit code
第2版
如果您不想编译,只想通过fsi运行它:
open System.IO
let fName = fsi.CommandLineArgs.[1] // you need the second item from the argument list,
//because the first one is the script name itself
let readData (fName:string) =
use stream = new StreamReader(fName)
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
readData fName
System.Console.ReadKey() |> ignore
0 // return an integer exit code
opensystem.IO
让fName=fsi.CommandLineArgs。[1]//您需要参数列表中的第二项,
//因为第一个是脚本名本身
let readData(fName:string)=
使用流=新的流阅读器(fName)
设可变有效=true
printfn“请输入要搜索的号码:”
让mutable x=System.Console.ReadLine()
设可变线=1
虽然(有效)做什么
let line=stream.ReadLine()
如果(line=null),则
你试过什么?你的目标(预期结果)是什么?你遇到了什么具体问题?不清楚你在问什么。我只能猜测,您希望此代码在一个可执行程序中运行,文件路径(此处D:\Searcher.txt
)作为命令行参数传递给该程序;对吗?
open System.IO
let fName = fsi.CommandLineArgs.[1] // you need the second item from the argument list,
//because the first one is the script name itself
let readData (fName:string) =
use stream = new StreamReader(fName)
let mutable valid = true
printfn "Please enter the number you want to search for: "
let mutable x = System.Console.ReadLine()
let mutable lines = 1
while (valid ) do
let line = stream.ReadLine()
if (line = null) then
valid <- false
else if (line = x) then
printfn "String found: %s" x
printfn "Found on: Line %i " lines
valid <- false
else
lines <- lines + 1
readData fName
System.Console.ReadKey() |> ignore
0 // return an integer exit code