add flags for output format

This commit is contained in:
filifa 2025-05-03 22:42:09 -04:00
parent 4ec04b6abf
commit 8855bc9932
1 changed files with 26 additions and 2 deletions

View File

@ -29,6 +29,11 @@ import (
var file string
var matlabFmt bool
var pythonFmt bool
var oneline bool
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "dptdist",
@ -64,10 +69,23 @@ func parse(cmd *cobra.Command, args []string) {
panic("not an absorbing Markov chain!")
}
fmtOptions := make([]mat.FormatOption, 0)
if matlabFmt {
fmtOptions = append(fmtOptions, mat.FormatMATLAB())
} else if pythonFmt {
fmtOptions = append(fmtOptions, mat.FormatPython())
}
fmtOptions = append(fmtOptions, mat.Squeeze())
a := graph.AdjacencyMatrix()
out := mat.Formatted(a)
out := mat.Formatted(a, fmtOptions...)
if (matlabFmt || pythonFmt) != oneline {
fmt.Printf("%#v\n", out)
} else {
fmt.Printf("%v\n", out)
}
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
@ -91,4 +109,10 @@ func init() {
rootCmd.Flags().StringVarP(&file, "file", "f", "", "dot file with absorbing Markov chain")
rootCmd.MarkFlagRequired("file")
rootCmd.Flags().BoolVar(&matlabFmt, "matlab", false, "format output as MATLAB array")
rootCmd.Flags().BoolVar(&pythonFmt, "python", false, "format output as python array")
rootCmd.MarkFlagsMutuallyExclusive("matlab", "python")
rootCmd.Flags().BoolVar(&oneline, "oneline", false, "output on one line")
}