How to use Optional method of prog Package

Best Syzkaller code snippet using prog.Optional

getopt_test.go

Source:getopt_test.go Github

copy

Full Screen

...86 Definitions{87 {"debug|d", "debug mode", Flag, true},88 {"verbose|v", "verbose mode", Flag, true},89 {"dryrun|D", "dry run only", Flag, true},90 {"logfile|l", "log file", Optional, ""},91 {"mode|m", "operating mode", Required, ""},92 },93 }94 os.Args = []string{"prog", "-dv"}95 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true {96 t.Errorf("did not recognize all flags when concatenation options (2 options)")97 }98 os.Args = []string{"prog", "-dvD"}99 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true || opts["dryrun"].Bool != true {100 t.Errorf("did not recognize all flags when concatenation options (3 options)")101 }102 os.Args = []string{"prog", "-dvD"}103 if _, _, _, err := options.ParseCommandLine(); err == nil || err.ErrorCode != MissingOption {104 t.Errorf("did not recognize a missing required option in concatenation mode")105 }106 os.Args = []string{"prog", "-Dl"}107 if _, _, _, err := options.ParseCommandLine(); err == nil || err.ErrorCode != MissingValue {108 t.Errorf("did not realize that I missed a value")109 }110 os.Args = []string{"prog", "-Dl", "-d"}111 if _, _, _, err := options.ParseCommandLine(); err == nil || err.ErrorCode != MissingValue {112 t.Errorf("did not realize that I missed a value")113 }114}115func TestConcatenatedOptionsParsingWithStringValueOptionAtTheEnd(t *testing.T) {116 options := Options{117 "",118 Definitions{119 {"debug|d", "debug mode", Flag, true},120 {"verbose|v", "verbose mode", Flag, true},121 {"dryrun|D", "dry run only", Flag, true},122 {"logfile|l", "log file", Optional, ""},123 {"mode|m", "operating mode", Required, ""},124 },125 }126 os.Args = []string{"prog", "-dvDl/tmp/log.txt"}127 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||128 opts["dryrun"].Bool != true || opts["logfile"].String != "/tmp/log.txt" {129 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 optional)")130 }131 os.Args = []string{"prog", "-dvDl", "/tmp/log.txt"}132 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||133 opts["dryrun"].Bool != true || opts["logfile"].String != "/tmp/log.txt" {134 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 optional separated by space)")135 }136 os.Args = []string{"prog", "-dvDmdaemon"}137 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||138 opts["dryrun"].Bool != true || opts["mode"].String != "daemon" {139 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 required)")140 }141 os.Args = []string{"prog", "-dvDm", "daemon"}142 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||143 opts["dryrun"].Bool != true || opts["mode"].String != "daemon" {144 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 required separated by space)")145 }146}147func TestConcatenatedOptionsParsingWithIntValueOptionAtTheEnd(t *testing.T) {148 options := Options{149 "",150 Definitions{151 {"debug|d", "debug mode", Flag, true},152 {"verbose|v", "verbose mode", Flag, true},153 {"dryrun|D", "dry run only", Flag, true},154 {"port|p", "port", Optional, 3000},155 {"instances|i", "instances", Required, 1},156 },157 }158 os.Args = []string{"prog", "-dvDp3000"}159 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||160 opts["dryrun"].Bool != true || opts["port"].Int != 3000 {161 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 optional int)")162 }163 os.Args = []string{"prog", "-dvDp", "3000"}164 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||165 opts["dryrun"].Bool != true || opts["port"].Int != 3000 {166 fmt.Printf("%#v", opts)167 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 optional int separated by space)")168 }169 os.Args = []string{"prog", "-dvDp", "3000"}170 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||171 opts["dryrun"].Bool != true || opts["port"].Int != 3000 {172 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 optional int separated by space)")173 }174 os.Args = []string{"prog", "-dvDi4"}175 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||176 opts["dryrun"].Bool != true || opts["instances"].Int != 4 {177 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 required)")178 }179 os.Args = []string{"prog", "-dvDi", "4"}180 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||181 opts["dryrun"].Bool != true || opts["instances"].Int != 4 {182 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 required int separated by space)")183 }184}185func TestConcatenatedOptionsParsingWithIntArrayValueOptionAtTheEnd(t *testing.T) {186 options := Options{187 "",188 Definitions{189 {"debug|d", "debug mode", Flag, true},190 {"verbose|v", "verbose mode", Flag, true},191 {"dryrun|D", "dry run only", Flag, true},192 {"ports|p", "ports", Optional, []int{3000, 3001, 3002}},193 {"timeouts|t", "timeouts", Required, []int{1, 2, 4, 10, 30}},194 },195 }196 os.Args = []string{"prog", "-dvDp5000,5001,5002"}197 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||198 opts["dryrun"].Bool != true || !equalIntArray(opts["ports"].IntArray, []int64{5000, 5001, 5002}) {199 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 optional int array)")200 }201 os.Args = []string{"prog", "-dvDp", "5000,5001,5002"}202 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||203 opts["dryrun"].Bool != true || !equalIntArray(opts["ports"].IntArray, []int64{5000, 5001, 5002}) {204 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 optional int array separated by space)")205 }206 os.Args = []string{"prog", "-dvDt10,20,30"}207 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||208 opts["dryrun"].Bool != true || !equalIntArray(opts["timeouts"].IntArray, []int64{10, 20, 30}) {209 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 required int array)")210 }211 os.Args = []string{"prog", "-dvDt", "10,20,30"}212 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true || opts["verbose"].Bool != true ||213 opts["dryrun"].Bool != true || !equalIntArray(opts["timeouts"].IntArray, []int64{10, 20, 30}) {214 t.Errorf("did not recognize all flags when concatenation options (3 flags + 1 required int array separated by space)")215 }216}217func TestEnvironmentValueParsing(t *testing.T) {218 options := Options{219 "",220 Definitions{221 {"debug|d|DEBUG", "debug mode", Flag, true},222 {"ports|p|PORTS", "ports", Required, []int{3000, 3001, 3002}},223 {"instances||INSTANCES", "instances", Optional, 4},224 {"keys||KEYS", "keys", Optional, []string{"foo,bar,baz"}},225 {"logfile||LOGFILE", "ports", Optional | ExampleIsDefault, "/var/log/foo.log"},226 {"hostname|h|HOSTNAME", "hostname", Optional | ExampleIsDefault | NoLongOpt, "/var/log/foo.log"},227 },228 }229 os.Args = []string{"prog"}230 os.Setenv("DEBUG", "1")231 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true {232 t.Errorf("did not recognize option set via ENV variable (DEBUG=1)")233 }234 os.Args = []string{"prog"}235 os.Setenv("DEBUG", "TRUE")236 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true {237 t.Errorf("did not recognize option set via ENV variable (DEBUG=TRUE)")238 }239 os.Args = []string{"prog"}240 os.Setenv("DEBUG", "true")241 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true {242 t.Errorf("did not recognize option set via ENV variable (DEBUG=true)")243 }244 os.Args = []string{"prog"}245 os.Setenv("PORTS", "4000,4001,4002")246 if opts, _, _, _ := options.ParseCommandLine(); !equalIntArray(opts["ports"].IntArray, []int64{4000, 4001, 4002}) {247 t.Errorf("did not recognize option set via ENV variable (PORTS=4000,4001,4002)")248 }249 os.Args = []string{"prog"}250 os.Setenv("KEYS", "faa,bor,boz")251 if opts, _, _, _ := options.ParseCommandLine(); !equalStringArray(opts["keys"].StrArray, []string{"faa", "bor", "boz"}) {252 t.Errorf("did not recognize option set via ENV variable (KEYS=faa,bor,boz)")253 }254 os.Args = []string{"prog"}255 os.Setenv("LOGFILE", "/tmp/logfile")256 if opts, _, _, _ := options.ParseCommandLine(); opts["logfile"].String != "/tmp/logfile" {257 t.Errorf("did not recognize option set via ENV variable (LOGFILE=/tmp/lofile)")258 }259 os.Args = []string{"prog"}260 os.Setenv("INSTANCES", "13")261 if opts, _, _, _ := options.ParseCommandLine(); opts["instances"].Int != 13 {262 t.Errorf("did not recognize option set via ENV variable (INSTANCES=13)")263 }264 os.Args = []string{"prog"}265 os.Setenv("LOGFILE", " /tmp/logfile ")266 if opts, _, _, _ := options.ParseCommandLine(); opts["logfile"].String != "/tmp/logfile" {267 t.Errorf("did not recognize option set via ENV variable with whitespace (LOGFILE=/tmp/lofile)")268 }269 os.Args = []string{"prog"}270 os.Setenv("INSTANCES", " 13 ")271 if opts, _, _, _ := options.ParseCommandLine(); opts["instances"].Int != 13 {272 t.Errorf("did not recognize option set via ENV variable with whitespace (INSTANCES=13)")273 }274 os.Args = []string{"prog"}275 os.Setenv("HOSTNAME", "eberhard")276 if opts, _, _, _ := options.ParseCommandLine(); opts["hostname"].String != "eberhard" {277 t.Errorf("did not recognize NoLongOpt option set via ENV variable")278 }279}280func TestDefaultValueParsing(t *testing.T) {281 options := Options{282 "",283 Definitions{284 {"debug|d|DEBUG", "debug mode", Optional | ExampleIsDefault, true},285 {"ports|p|PORTS", "ports", Optional | ExampleIsDefault, []int64{3000, 3001, 3002}},286 {"secondaryports|s|SECONDARY_PORTS", "secondary ports", Optional | ExampleIsDefault, []int{5000, 5001, 5002}},287 {"instances||INSTANCES", "instances", Optional | ExampleIsDefault, 4},288 {"keys||KEYS", "keys", Optional | ExampleIsDefault, []string{"foo", "bar", "baz"}},289 {"logfile||LOGFILE", "logfile", Optional | ExampleIsDefault, "/var/log/foo.log"},290 },291 }292 os.Args = []string{"prog"}293 os.Setenv("INSTANCES", "")294 os.Setenv("KEYS", "")295 os.Setenv("LOGFILE", "")296 if opts, _, _, _ := options.ParseCommandLine(); opts["instances"].Int != 4 {297 t.Errorf("did not recognize default value (int instances)")298 }299 if opts, _, _, _ := options.ParseCommandLine(); !equalStringArray(opts["keys"].StrArray, []string{"foo", "bar", "baz"}) {300 t.Errorf("did not recognize default value (string array keys)")301 }302 if opts, _, _, _ := options.ParseCommandLine(); opts["logfile"].String != "/var/log/foo.log" {303 t.Errorf("did not recognize default value (string logfile)")304 }305 if opts, _, _, _ := options.ParseCommandLine(); opts["debug"].Bool != true {306 t.Errorf("did not recognize default value (boolean debug)")307 }308 if opts, _, _, _ := options.ParseCommandLine(); !equalIntArray(opts["ports"].IntArray, []int64{3000, 3001, 3002}) {309 t.Errorf("did not recognize default value (int64 array ports)")310 }311 if opts, _, _, _ := options.ParseCommandLine(); !equalIntArray(opts["secondaryports"].IntArray, []int64{5001, 5002, 5003}) {312 t.Errorf("did not recognize default value (int array ports)")313 }314}315func TestArgumentsParsing(t *testing.T) {316 options := Options{317 "",318 Definitions{319 {"debug|d|DEBUG", "debug mode", Flag, true},320 {"ports|p|PORTS", "ports", Optional | ExampleIsDefault, []int64{3000, 3001, 3002}},321 },322 }323 os.Args = []string{"prog", "-d", "foobar", "barbaz"}324 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"foobar", "barbaz"}) || opts["debug"].Bool != true {325 t.Errorf("did not recognize arguments (two at the end)")326 }327 os.Args = []string{"prog", "foobar", "-d", "barbaz"}328 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"foobar", "barbaz"}) || opts["debug"].Bool != true {329 t.Errorf("did not recognize arguments separated by bool option")330 }331 os.Args = []string{"prog", "foobar", "barbaz", "-d"}332 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"foobar", "barbaz"}) || opts["debug"].Bool != true {333 t.Errorf("did not recognize arguments before option")334 }335 os.Args = []string{"prog", "-d", "-p5000,6000", "foobar", "barbaz"}336 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"foobar", "barbaz"}) ||337 opts["debug"].Bool != true ||338 !equalIntArray(opts["ports"].IntArray, []int64{5000, 6000}) {339 t.Errorf("parsing error of command line: '-d -p5000,6000 foobar barbaz'")340 }341 os.Args = []string{"prog", "-dp5000,6000", "foobar", "barbaz"}342 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"foobar", "barbaz"}) ||343 opts["debug"].Bool != true ||344 !equalIntArray(opts["ports"].IntArray, []int64{5000, 6000}) {345 t.Errorf("parsing error of command line: '-dp5000,6000 foobar barbaz'")346 }347 os.Args = []string{"prog", "-d", "foobar", "-p5000,6000", "barbaz"}348 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"foobar", "barbaz"}) ||349 opts["debug"].Bool != true ||350 !equalIntArray(opts["ports"].IntArray, []int64{5000, 6000}) {351 t.Errorf("parsing error of command line: '-d foobar -p5000,6000 barbaz'")352 }353 os.Args = []string{"prog", "-p5000,6000", "foobar", "-d", "barbaz"}354 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"foobar", "barbaz"}) ||355 opts["debug"].Bool != true ||356 !equalIntArray(opts["ports"].IntArray, []int64{5000, 6000}) {357 t.Errorf("parsing error of command line: '-p5000,6000 foobar -d barbaz'")358 }359 os.Args = []string{"prog", "barbaz", "-d", "-p5000,6000", "foobar"}360 if opts, arguments, _, _ := options.ParseCommandLine(); !equalStringArray(arguments, []string{"barbaz", "foobar"}) ||361 opts["debug"].Bool != true ||362 !equalIntArray(opts["ports"].IntArray, []int64{5000, 6000}) {363 fmt.Printf("args: %#v\nopts: %#v\nports: %#v\n", arguments, opts["debug"].Bool, opts["ports"].IntArray)364 t.Errorf("parsing error of command line: 'barbaz -d -p5000,6000 foobar'")365 }366}367func TestPassThroughParsing(t *testing.T) {368 options := Options{369 "",370 Definitions{371 {"debug|d|DEBUG", "debug mode", Flag, true},372 {"ports|p|PORTS", "ports", Optional | ExampleIsDefault, []int64{3000, 3001, 3002}},373 {"command args", "command args", Required | IsPassThrough, "command"},374 },375 }376 os.Args = []string{"prog"}377 if _, _, _, err := options.ParseCommandLine(); err != nil {378 t.Errorf("missing pass through rose error: %#v", err)379 }380 os.Args = []string{"prog", "foobar", "--", "barbaz"}381 expected := []string{"barbaz"}382 if _, _, passThrough, _ := options.ParseCommandLine(); !equalStringArray(passThrough, expected) {383 t.Errorf("simple pass through not recognized:\ngot: |" + fmt.Sprintf("%#v", passThrough) + "|\nexpected: |" + fmt.Sprintf("%#v", expected) + "|\n")384 }385 os.Args = []string{"prog", "foobar", "--", "ls", "-lah", "/tmp"}386 expected = []string{"ls", "-lah", "/tmp"}...

Full Screen

Full Screen

sam.go

Source:sam.go Github

copy

Full Screen

1// Copyright (C) 2012 Phillip Garland <pgarland@gmail.com>2// This program is free software: you can redistribute it and/or3// modify it under the terms of the GNU Lesser General Public License4// as published by the Free Software Foundation, either version 3 of5// the License, or (at your option) any later version.6// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License for more details.10// You should have received a copy of the GNU Lesser General Public11// License along with this program. If not, see12// <http://www.gnu.org/licenses/>.13package goSAM14import (15 "fmt"16 "os"17 "bufio"18 "strings"19 "strconv"20 "container/list"21 "regexp"22)23type HeaderLine struct {24 Version string // VN | /^[0-9]+\.[0-9]+$/ | required25 SortOrder string // SO | unknown, unsorted, queryname, coordinate | optional26}27func validateHeader(hl *HeaderLine) (bool, error) {28 m, _ := regexp.Match("^[0-9]+.[0-9]+$", []byte(hl.Version))29 if !m {30 return m, SAMerror{"Invalid version in SAM Header"}31 } 32 return m, nil33}34var hlParseMap = map[string]func(string, *HeaderLine) {35 "VN": func(val string, hl *HeaderLine) {hl.Version = val},36 "SO": func(val string, hl *HeaderLine) {hl.SortOrder = val},37}38func parseHeader(line string) *HeaderLine {39 tvs := strings.Split(line, "\t")40 hl := HeaderLine{}41 for _,tv := range tvs[1:] {42 tva := strings.Split(tv,":")43 tag := tva[0]44 val := tva[1]45 parseFunc := hlParseMap[tag]46 if parseFunc != nil {47 parseFunc(val, &hl)48 } // FIXME: catch and collect non-std tags?49 }50 return &hl51}52// Order of SQ lines defines the alignment sorting order53type RefSeqDict struct {54 Name string // SN | [!-)+-<>-~][!-~]* | required | unique55 Length uint32 // LN | Range: [1, 2^29 -1] | required56 AssemblyID string // AS | optional57 MD5 string // M5 | optional58 Species string // SP | optional59 URI string // || UR | optional | use URL type?60}61func validateRefSeqDict(rsd *RefSeqDict) (bool, error) {62 m , _ := regexp.Match("[!-)+-<>-~][!-~]*", []byte(rsd.Name))63 if !m {64 return false, SAMerror{"Invalid reference sequence name"}65 }66 return ((rsd.Length >= 1) && (rsd.Length <= 0x1FFFFFFF)), nil67}68func parseRefSeqDict(line string) *RefSeqDict {69 tvs := strings.Split(line, "\t")70 rsd := RefSeqDict{}71 for _,tv := range tvs[1:] {72 tva := strings.Split(tv,":")73 switch tag := tva[0]; tag {74 case "SN":75 rsd.Name = tva[1]76 case "LN":77 v, _ := strconv.Atoi(tva[1])78 rsd.Length = uint32(v)79 case "AS":80 rsd.AssemblyID = tva[1]81 case "M5":82 rsd.MD5 = tva[1]83 case "SP":84 rsd.Species = tva[1]85 case "UR":86 rsd.URI = tva[1]87 }88 }89 return &rsd90}91type ReadGroup struct {92 ID string // ID | unique | required93 SeqCenter string // CN | optional 94 Description string // DS | optional95 Date string // DT | optional96 FlowOrder string // FO | /\*|[ACMGRSVTWYHKDBN]+/ | optional97 KeySeq string // KS | optional98 Lib string // LB | optional99 Programs string // PG | optional100 PMIS string // PI | optional | predicted median insert size101 Platform string // PL | CAPILLARY LS454 ILLUMINA SOLID HELICOS IONTORRENT PACBIO | optional102 Unit string // PU | Unique | optional103 Sample string // SM | optional104}105// The usefulness of checking platforms seems dubious to me. What106// happens as new platforms come into use. I may skip checking this if107// it causes problems.108var validPlatforms = map[string]bool{109 "CAPILLARY": true,110 "LS454": true,111 "ILLUMINA": true,112 "SOLID": true, 113 "HELICOS": true, 114 "IONTORRENT": true,115 "PACBIO": true,116}117// FIXME: make sure ID is unique118func validateReadGroup (rg *ReadGroup) (bool, error) {119 m := true120 // FlowOrder is optional, so we have to check it's existence121 // first, though I guess I could just include the empty string as122 // an alternative in the match.123 if rg.FlowOrder != "" {124 m, _ = regexp.Match("*|[ACMGRSVTWYHKDBN]+",[]byte(rg.FlowOrder))125 if !m {126 return false, SAMerror{"Invalid flow order in read group"}127 }128 }129 if rg.Platform != "" {130 m = validPlatforms[rg.Platform]131 if !m {return false, SAMerror{"Invalid platform in read group"}}132 }133 return true, nil134}135var rgParseMap = map[string]func(string, *ReadGroup) {136 "ID": func(s string, rg *ReadGroup) {rg.ID = s},137 "CN": func(s string, rg *ReadGroup) {rg.SeqCenter = s},138 "DS": func(s string, rg *ReadGroup) {rg.Description = s},139 "DT": func(s string, rg *ReadGroup) {rg.Date = s},140 "FO": func(s string, rg *ReadGroup) {rg.FlowOrder = s},141 "KS": func(s string, rg *ReadGroup) {rg.KeySeq = s},142 "LB": func(s string, rg *ReadGroup) {rg.Lib = s},143 "PG": func(s string, rg *ReadGroup) {rg.Programs = s},144 "PI": func(s string, rg *ReadGroup) {rg.PMIS = s},145 "PL": func(s string, rg *ReadGroup) {rg.Platform = s},146 "PU": func(s string, rg *ReadGroup) {rg.Unit = s},147 "SM": func(s string, rg *ReadGroup) {rg.Sample = s},148}149func parseReadGroup(line string) *ReadGroup {150 tvs := strings.Split(line, "\t")151 rg := ReadGroup{}152 for _,tv := range tvs[1:] {153 tva := strings.Split(tv,":")154 tag := tva[0]155 val := tva[1]156 parseFunc := rgParseMap[tag]157 if parseFunc != nil {158 parseFunc(val, &rg)159 } // FIXME: catch and collect non-std tags?160 }161 return &rg162}163type Program struct {164 ID string // ID | unique | required165 Name string // PN | optional166 CmdLine string // CL | optional167 PrevID string // PP | must match another PG line ID | optional168}169func validateProgram(prog *Program) (bool, error) {170 if prog.ID == "" {return false, SAMerror{"Program ID is required"}}171 return true, nil172}173var programParseMap = map[string]func(string, *Program) {174 "ID": func(s string, prog *Program) {prog.ID = s},175 "PN": func(s string, prog *Program) {prog.Name = s},176 "CL": func(s string, prog *Program) {prog.CmdLine = s},177 "PP": func(s string, prog *Program) {prog.PrevID = s},178} 179func parseProgram(line string) *Program {180 tvs := strings.Split(line, "\t")181 prog := Program{}182 for _,tv := range tvs[1:] {183 tva := strings.Split(tv,":")184 tag := tva[0]185 val := tva[1]186 parseFunc := programParseMap[tag]187 if parseFunc != nil {188 parseFunc(val, &prog)189 } // FIXME: catch and collect non-std tags?190 }191 return &prog192}193type Alignment struct {194 Qname string // required | [!-?A-~]{1-255} | query template name195 Flag uint16 // required | [0-2^16 - 1] | bitwise flag196 RefName string // required | \*|[!-()+-<>-~][!-~]*197 Pos uint32 // required | [0-2^29-1]198 Mapq uint8 // required | [0-2^8-1]199 Cigar string // required | \*|([0-9]+[MIDNSHPX=])+200 NextRef string // required | \*|=|[!-()+-<>-~][!-~]*201 NextPos uint32 // required | [0-2^29-1]202 TemplateLen int32 // required | [-2^29+1 - 2^29-1]203 Seq string // required | \*|[A-Za-z=.]+204 Qual string // required ASCII Phred score+33205}206// FIXME: These regexp patterns should be compiled, since they'll be207// used over and over208func validateAlignment(a *Alignment) (bool, error){209 if m, _ := regexp.Match("*|[!-?A-~]+", []byte(a.Qname)); !m {210 return false, SAMerror{"Invalid qname in alignment"}211 }212 if (a.Flag < 0 || a.Flag > 0xFFFF) {213 return false, SAMerror{"Invalid flag in alignment"}214 }215 if m, _ := regexp.Match("*|[!-()+-<>-~][!-~]*", []byte(a.RefName)); !m {216 return false, SAMerror{"Invalid reference sequence name in alignment"}217 }218 if a.Pos < 0 || a.Pos > 0x1FFFFFFF {219 return false, SAMerror{"Alignment mapping position out of valid range"}220 }221 if a.Mapq < 0 || a.Mapq > 0xFF {222 return false, SAMerror{"Alignment mapping quality out of valid range"}223 }224 if m, _ := regexp.Match("*|([0-9]+[MIDNSHPX=])+", []byte(a.Cigar)); !m { 225 return false, SAMerror{"Invalid CIGAR string in alignment"}226 }227 if m, _ := regexp.Match("*|=|[!-()+-<>-~][!-~]*", []byte(a.NextRef)); !m {228 return false, SAMerror{"Invalid next reference name in alignment"}229 }230 if a.NextPos < 0 || a.NextPos > 0x1FFFFFFF {231 return false, SAMerror{"Alignment mapping position out of valid range"}232 }233 if a.TemplateLen < -0x1FFFFFFF || a.TemplateLen > 0x1FFFFFFF {234 return false, SAMerror{"Invalid template length"}235 }236 if m, _ := regexp.Match("*|[A-Za-z=.]+",[]byte(a.Seq)); !m {237 return false, SAMerror{"Invalid sequence in alignment"}238 }239 if m, _ := regexp.Match("*|[!-~]+",[]byte(a.Qual)); !m {240 return false, SAMerror{"Invalie Phred quality in alignment"}241 } 242 return true, nil243}244func parseAlignment(line string) *Alignment {245 fields := strings.Split(line, "\t")246 alignment := Alignment{}247 alignment.Qname = fields[0]248 flagVal, _ := strconv.Atoi(fields[1])249 alignment.Flag = uint16(flagVal)250 alignment.RefName = fields[2]251 posVal, _ := strconv.Atoi(fields[3])252 alignment.Pos = uint32(posVal)253 mapqVal, _ := strconv.Atoi(fields[4])254 alignment.Mapq = uint8(mapqVal)255 alignment.Cigar = fields[5]256 alignment.NextRef = fields[6]257 nextPosVal, _ := strconv.Atoi(fields[7])258 alignment.NextPos = uint32(nextPosVal)259 templateLenVal, _ := strconv.Atoi(fields[8])260 alignment.TemplateLen = int32(templateLenVal) 261 alignment.Seq = fields[9]262 alignment.Qual = fields[10]263 return &alignment264}265func bitIsSet(bit uint8, bitmap uint16) {266 if (bitmap & bit) == bit {267 return true268 }269 return false270}271func hasMultipleSegments(a *Alignment) bool {272 return bitIsSet(0x01, a.Flag)273}274func segmentIsUnmapped(a *Alignment) bool {275 return bitIsSet(0x04, a.Flag)276}277type SAMerror struct {278 str string279}280func (e SAMerror) Error() string {281 return fmt.Sprintf("sam: %s", e.str)282}283func ReadSAMFile(fileName string) (*HeaderLine, *list.List, *list.List, *list.List, *list.List, error) {284 file, err := os.Open(fileName);285 if err != nil {286 fmt.Println(err)287 return nil, nil, nil, nil, nil, err288 }289 reader := bufio.NewReader(file)290 // These will be returned so they must be declared in this scope291 var header *HeaderLine292 var rsdl, rgl, progl, al = list.New(), list.New(), list.New(), list.New()293 // Maps to keep track of values that must be unique. Used for checking for duplicate values.294 var rsdNames, rgIDs, progIDs = map[string]bool{}, map[string]bool{}, map[string]bool{}295 // separating the cases into separate handler functions doesn't296 // seem to win much, so I'm leaving this as it is for now, though297 // it is longer than I'd like.298 for line, _, err := reader.ReadLine(); err == nil; line, _, err = reader.ReadLine() {299 s := string(line)300 switch lineTag := s[1:3]; lineTag {301 case "HD": 302 header = parseHeader(s)303 if valid, err := validateHeader(header); !valid {304 return header, nil, nil, nil, nil, err305 }306 case "SQ":307 rsd := parseRefSeqDict(s)308 if valid, err := validateRefSeqDict(rsd); !valid {309 return header, nil, nil, nil, nil, err310 } else { 311 if rsdNames[rsd.Name] { // Make sure name is unique312 return header, rsdl, nil, nil, nil, SAMerror{"Reference sequence name is not unique"}313 } else { // Everything is OK314 rsdNames[rsd.Name] = true315 rsdl.PushBack(rsd)316 }317 }318 case "RG":319 rg := parseReadGroup(s)320 if valid, err := validateReadGroup(rg); !valid {321 return header, rsdl, rgl, nil, nil, err322 } else {323 if rgIDs[rg.ID] {324 return header, rsdl, rgl, nil, nil, SAMerror{"Read group name is not unique"}325 } else {326 rgIDs[rg.ID] = true327 rgl.PushBack(rg)328 }329 }330 case "PG":331 prog := parseProgram(s)332 if valid, err := validateProgram(prog); !valid {333 return header, rsdl, rgl, progl, nil, err334 } else {335 if progIDs[prog.ID] {336 return header, rsdl, rgl, progl, nil, SAMerror{"Program ID is not unique"}337 } else {338 progIDs[prog.ID] = true339 progl.PushBack(prog)340 }341 }342 case "CO":343 // FIXME: It should be possible for the QNAME field of an344 // alignment to have "HD", "SQ", "RG", "PG", or "CO" as345 // characters 1 and 2, so making alignment the default346 // lone type is not right.347 default: 348 a := parseAlignment(s)349 if valid, err := validateAlignment(a); !valid {350 return header, rsdl, rgl, progl, al , err351 } else {352 al.PushBack(a)353 }354 }355 }356 file.Close()357 return header, rsdl, rgl, progl, al, err358}359func ReadNextAlignment() {360}...

Full Screen

Full Screen

optional.go

Source:optional.go Github

copy

Full Screen

1package flags2import "strconv"3// Gah! Boilerplate everywhere!4// OptionalString is a string flag that lets you to tell if the flag has been5// set or not, unlike flag.StringVar(), which does not.6//7// OptionalString can distinguish between all three of these conditions:8//9// $ prog -flag foo10// $ prog -flag ""j11// $ prog12//13type OptionalString struct {14 IsSet bool15 Value string16}17func (s *OptionalString) Set(x string) error {18 s.Value = x19 s.IsSet = true20 return nil21}22func (s *OptionalString) String() string {23 if s == nil {24 return ""25 }26 return s.Value27}28// OptionalInt is an int flag that lets you to tell if the flag has been29// set or not, unlike flag.IntVar(), which does not.30//31// OptionalInt can distinguish between all three of these conditions:32//33// $ prog -int 134// $ prog -int 035// $ prog -int ""36//37type OptionalInt struct {38 IsSet bool39 Value int40 str string41}42func (s *OptionalInt) Set(x string) error {43 i, err := strconv.ParseInt(x, 10, 0)44 if err != nil {45 return err46 }47 s.Value = int(i)48 s.IsSet = true49 s.str = x50 return nil51}52func (s *OptionalInt) String() string {53 if s == nil {54 return ""55 }56 return s.str57}58// OptionalInt64 is an int flag that lets you to tell if the flag has been59// set or not, unlike flag.Int64Var(), which does not.60//61// OptionalInt64 can distinguish between all three of these conditions:62//63// $ prog -int64 164// $ prog -int64 065// $ prog -int64 ""66//67type OptionalInt64 struct {68 IsSet bool69 Value int6470 str string71}72func (s *OptionalInt64) Set(x string) error {73 i, err := strconv.ParseInt(x, 10, 64)74 if err != nil {75 return err76 }77 s.Value = i78 s.IsSet = true79 s.str = x80 return nil81}82func (s *OptionalInt64) String() string {83 if s == nil {84 return ""85 }86 return s.str87}88// OptionalUint is an int flag that lets you to tell if the flag has been89// set or not, unlike flag.UintVar(), which does not.90//91// OptionalUint can distinguish between all three of these conditions:92//93// $ prog -uint 194// $ prog -uint 095// $ prog -uint ""96//97type OptionalUint struct {98 IsSet bool99 Value uint100 str string101}102func (s *OptionalUint) Set(x string) error {103 i, err := strconv.ParseUint(x, 10, 0)104 if err != nil {105 return err106 }107 s.Value = uint(i)108 s.IsSet = true109 s.str = x110 return nil111}112func (s *OptionalUint) String() string {113 if s == nil {114 return ""115 }116 return s.str117}118// OptionalUint64 is an int flag that lets you to tell if the flag has been119// set or not, unlike flag.Uint64Var(), which does not.120//121// OptionalUint64 can distinguish between all three of these conditions:122//123// $ prog -uint64 1124// $ prog -uint64 0125// $ prog126//127type OptionalUint64 struct {128 IsSet bool129 Value uint64130 str string131}132func (s *OptionalUint64) Set(x string) error {133 i, err := strconv.ParseUint(x, 10, 64)134 if err != nil {135 return err136 }137 s.Value = i138 s.IsSet = true139 s.str = x140 return nil141}142func (s *OptionalUint64) String() string {143 if s == nil {144 return ""145 }146 return s.str147}148// OptionalBool is an bool flag that lets you to tell if the flag has been149// set or not, unlike flag.BoolVar(), which does not.150//151// OptionalBool can distinguish between all three of these conditions:152//153// $ prog -bool=true154// $ prog -bool=false155// $ prog156//157// OptionalBool supports, but can not distinguish, this additional condition:158//159// $ prog -bool160//161// Like flag.BoolVar(), OptionalBool does not support these conditions:162//163// $ prog -bool true164// $ prog -bool false165//166type OptionalBool struct {167 IsSet bool168 Value bool169}170func (s *OptionalBool) IsBoolFlag() bool { return true }171func (s *OptionalBool) Set(x string) error {172 b, err := strconv.ParseBool(x)173 if err != nil {174 return err175 }176 s.Value = b177 s.IsSet = true178 return nil179}180func (s *OptionalBool) String() string {181 if s == nil {182 return ""183 }184 if !s.IsSet {185 return ""186 } else if s.Value {187 return "true"188 } else {189 return "false"190 }191}192// OptionalFloat64 is a float64 flag that lets you to tell if the flag has been193// set or not, unlike flag.Float64Var(), which does not.194//195// OptionalFloat64 can distinguish between all three of these conditions:196//197// $ prog -float64 1.1198// $ prog -float64 0199// $ prog200//201type OptionalFloat64 struct {202 IsSet bool203 Value float64204 str string205}206func (s *OptionalFloat64) Set(x string) error {207 f, err := strconv.ParseFloat(x, 64)208 if err != nil {209 return err210 }211 s.Value = f212 s.IsSet = true213 s.str = x214 return nil215}216func (s *OptionalFloat64) String() string {217 if s == nil {218 return ""219 }220 return s.str221}...

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("test.txt")4 if err != nil {5 log.Fatal(err)6 }7 defer f.Close()8 scanner := bufio.NewScanner(f)9 scanner.Split(bufio.ScanWords)10 for scanner.Scan() {11 }12 if err := scanner.Err(); err != nil {13 log.Fatal(err)14 }15 fmt.Printf("File has %d words", wordCount)16}17import (18type Person struct {19}20func main() {21 f, err := os.Open("test.txt")22 if err != nil {23 log.Fatal(err)24 }25 defer f.Close()26 scanner := bufio.NewScanner(f)27 for scanner.Scan() {28 line := scanner.Text()29 parts := strings.Split(line, " ")30 if len(parts) != 2 {31 log.Fatalf("invalid input: %q", line)32 }33 people = append(people, Person{34 })35 }36 if err := scanner.Err(); err != nil {37 log.Fatal(err)38 }39 for _, p := range people {40 fmt.Printf("%s %s\n", p.firstName, p.lastName

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import (2type Prog struct {3}4func (p Prog) Optional() *Prog {5 if p.value == 0 {6 }7}8func main() {9 if len(os.Args) != 2 {10 fmt.Println("Usage: 2.go <number>")11 os.Exit(1)12 }13 n, _ := strconv.Atoi(os.Args[1])14 p := Prog{n}15 if p.Optional() == nil {16 fmt.Println("p is nil")17 } else {18 fmt.Println("p is not nil")19 }20}21import (22type Prog struct {23}24func (p Prog) Optional() *Prog {25 if p.value == 0 {26 }27}28func main() {29 if len(os.Args) != 2 {30 fmt.Println("Usage: 3.go <number>")31 os.Exit(1)32 }33 n, _ := strconv.Atoi(os.Args[1])34 p := Prog{n}35 if p.Optional() == nil {36 fmt.Println("p is nil")37 } else {38 fmt.Println("p is not nil")39 }40}41import (42type Prog struct {43}44func (p Prog) Optional() *Prog {45 if p.value == 0 {46 }47}48func main() {49 if len(os.Args) != 2 {50 fmt.Println("Usage: 4.go <number>")51 os.Exit(1)52 }53 n, _ := strconv.Atoi(os.Args[1])54 p := Prog{n}55 if p.Optional() == nil {56 fmt.Println("p is nil")57 } else {58 fmt.Println("p is not nil")59 }60}61import (

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Print("Enter a number: ")4 fmt.Scanln(&a)5 fmt.Print("Enter another number: ")6 fmt.Scanln(&b)7 fmt.Println("Sum is: ", c)8}9import "fmt"10import "os"11import "strconv"12func main() {13 a, _ = strconv.Atoi(os.Args[1])14 b, _ = strconv.Atoi(os.Args[2])15 fmt.Println("Sum is: ", c)16}17import "fmt"18func main() {19 fmt.Print("Enter a number: ")20 fmt.Scanln(&a)21 fmt.Print("Enter another number: ")22 fmt.Scanln(&b)23 c = sum(a, b)24 fmt.Println("Sum is: ", c)25}26func sum(a int, b int) int {27}28import "fmt"29import "os"30import "strconv"31func main() {32 a, _ = strconv.Atoi(os.Args[1])33 b, _ = strconv.Atoi(os.Args[2])

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(prog.Optional())4}5import (6func main() {7 fmt.Println(prog.Optional())8}9import (10func main() {11 fmt.Println(prog.Optional())12}13import (14func main() {15 fmt.Println(prog.Optional())16}17import (18func main() {19 fmt.Println(prog.Optional())20}21import (22func main() {23 fmt.Println(prog.Optional())24}25import (26func main() {27 fmt.Println(prog.Optional())28}29import (30func main() {31 fmt.Println(prog.Optional())32}33import (34func main() {35 fmt.Println(prog.Optional())36}37import (38func main() {39 fmt.Println(prog.Optional())40}41import (42func main() {43 fmt.Println(prog.Optional

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 fmt.Println(golenv.Get("GOPATH"))5 fmt.Println(golprog.Optional("GOPATH", "default"))6}7import (8func main() {9 fmt.Println("Hello, playground")10 fmt.Println(golstring.Random(10))11 fmt.Println(golstring.Random(10))12 fmt.Println(golstring.Random(10))13}14import (15func main() {16 fmt.Println("Hello, playground")17 fmt.Println(goltime.Format("2016-01-02T15:04:05Z07:00"))18 fmt.Println(goltime.Format("2016-01-02T15:04:05Z07:00", "YYYY-MM-DD hh:mm:ss"))19 fmt.Println(goltime.Format("2016-01-02T15:04:05Z07:00", "YYYY-MM-DD hh:mm:ss", "IST"))20}21import (22func main() {23 fmt.Println("Hello, playground")24 fmt.Println(goltime.Format("

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter two numbers")4 fmt.Scan(&a, &b)5 fmt.Println("Sum of the two numbers is", prog.Sum(a, b))6}

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 p := prog{}4 p.Optional()5 p.Required()6}7type prog struct {8}9func (p prog) Optional() {10 fmt.Println("This is Optional")11}12func (p prog) Required() {13 fmt.Println("This is Required")14}

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(prog.Optional("Golang"))4}5import (6func main() {7 fmt.Println(prog.Pointer("Golang"))8}9import (10func main() {11 fmt.Println(prog.Pointer("Golang"))12}13import (14func main() {15 fmt.Println(prog.Pointer("Golang"))16}17import (18func main() {19 fmt.Println(prog.Pointer("Golang"))20}21import (22func main() {23 fmt.Println(prog.Pointer("Golang"))24}25import (26func main() {27 fmt.Println(prog.Pointer("Golang"))28}

Full Screen

Full Screen

Optional

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 _, err := exec.Command("ls", "-l").Output()4 if err != nil {5 fmt.Println("Program is not running")6 } else {7 fmt.Println("Program is running")8 }9}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful